示例#1
0
        private Dictionary <Position, long> ExecuteProgram(long[] program, long baseColor)
        {
            HullPaintingRobot           hpr           = new HullPaintingRobot(program);
            Dictionary <Position, long> positionColor =
                new Dictionary <Position, long>(new PositionEqualityComparer());

            long     input  = baseColor;
            Position hprPos = new Position(0, 0);

            positionColor[hprPos]   = baseColor;
            hpr.Run();
            hpr.Input = baseColor;

            while (hpr.CurrentState != IntcodeComputer.State.Halted)
            {
                // Color.
                hpr.Run();
                positionColor[hprPos] = hpr.Output;

                // Rotation.
                hpr.Run();
                hpr.Rotate(hpr.Output);

                hpr.Move();

                // Input.
                hpr.Run();
                hprPos = new Position(hpr.X, hpr.Y);
                positionColor.TryGetValue(hprPos, out input);
                hpr.Input = input;
            }

            return(positionColor);
        }
示例#2
0
        /// <summary>
        /// Execution function for Day 11
        /// </summary>
        public void Execute11()
        {
            UserActionAsync(() =>
            {
                WriteToConsole("Start execution of Day11");
                var parser       = GetInputParser("Day11Input.txt");
                var originalCode = parser.GetIntCode();
                var code         = originalCode.ToList();

                var hullPaintingRobot = new HullPaintingRobot(code);
                hullPaintingRobot.Paint(0);

                WriteToConsole($"The hull paining robot has painted {hullPaintingRobot.PaintedPanels.Count} at least once");

                WriteToConsole($"Now we start with a white panel instead of a black one.");

                hullPaintingRobot = new HullPaintingRobot(code);
                hullPaintingRobot.Paint(1);

                var painting = hullPaintingRobot.GetPainting();

                RunInUiThread(() =>
                {
                    var dialog = new ImageDisplay(painting);
                    dialog.Show();
                });
            });
        }
        public void Day11Part1Exercise()
        {
            var robot   = new HullPaintingRobot();
            var visited = robot.Paint();

            visited.Count.Should().Be(2255);
        }
示例#4
0
        public Result First(List <string> input)
        {
            var robot = new HullPaintingRobot(input.First());
            var res   = robot.CountPaintedTiles();

            return(new Result(res));
        }
        public void TestMoveUp()
        {
            HullPaintingRobot hpr = new HullPaintingRobot(new long[10]);

            hpr.Move();
            Assert.That(hpr.X, Is.EqualTo(0));
            Assert.That(hpr.Y, Is.EqualTo(-1));
        }
        public void Day11Part2Exercise()
        {
            var robot   = new HullPaintingRobot(1);
            var visited = robot.Paint();

            var numberPlate = new NumberPlate {
                Panels = visited
            };
            // BCKFPCRA
        }
示例#7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Day 11 - Space Police");
            Console.WriteLine("Star 1");
            Console.WriteLine();

            string line = File.ReadAllText(inputFile);

            long[] regs = line.Split(",").Select(long.Parse).ToArray();

            HullPaintingRobot firstRobot = new HullPaintingRobot(1);

            IntCode firstMachine = new IntCode(
                name: "Star 1",
                regs: regs,
                fixedInputs: Array.Empty <long>(),
                input: firstRobot.GetLocationColor,
                output: firstRobot.HandleInput);

            firstMachine.SyncRun();

            Console.WriteLine($"The answer is: {firstRobot.paintedLocations.Count}");

            Console.WriteLine();
            Console.WriteLine("Star 2");
            Console.WriteLine();

            HullPaintingRobot secondRobot = new HullPaintingRobot(2);

            IntCode secondMachine = new IntCode(
                name: "Star 2",
                regs: regs,
                fixedInputs: Array.Empty <long>(),
                input: secondRobot.GetLocationColor,
                output: secondRobot.HandleInput);

            secondMachine.SyncRun();

            HashSet <Point2D> finalLocations = new HashSet <Point2D>(
                secondRobot.paintedLocations.Where(x => x.Value == 1).Select(x => x.Key));

            Point2D min = finalLocations.MinCoordinate();
            Point2D max = finalLocations.MaxCoordinate() + (1, 1);

            Console.BackgroundColor = ConsoleColor.Black;
            for (int y = min.y; y < max.y; y++)
            {
                for (int x = min.x; x < max.x; x++)
                {
                    if (finalLocations.Contains((x, y)))
                    {
                        Console.BackgroundColor = ConsoleColor.White;
                    }
        public void TestRotateClockwise()
        {
            HullPaintingRobot hpr = new HullPaintingRobot(new long[10]);

            Assert.That(hpr.Direction, Is.EqualTo(0));
            hpr.Rotate(1);
            Assert.That(hpr.Direction, Is.EqualTo(1));
            hpr.Rotate(1);
            Assert.That(hpr.Direction, Is.EqualTo(2));
            hpr.Rotate(1);
            Assert.That(hpr.Direction, Is.EqualTo(3));
            hpr.Rotate(1);
            Assert.That(hpr.Direction, Is.EqualTo(0));
        }
示例#9
0
        public async Task Run_HullPaintingRobot(int[] output, int expectedResult)
        {
            var paintArea       = new Dictionary <Point, bool>();
            var intcodeComputer = Substitute.For <IIntcodeComputer>();
            var hullRobot       = new HullPaintingRobot(intcodeComputer, paintArea);

            var task = hullRobot.RunAsync();

            foreach (var o in output)
            {
                //simulate intcode outputs
                intcodeComputer.OnOutput += Raise.Event <Action <long> >((long)o);
            }

            await task;

            Assert.AreEqual(expectedResult, paintArea.Count);
        }
示例#10
0
        public void SolvePart1()
        {
            var input = System.IO.File.ReadAllText("../../../input/day_11.txt");

            long[] initialMemoryState = input
                                        .Split(',')
                                        .Select(x => long.Parse(x))
                                        .ToArray();

            var intcode           = new Intcode(initialMemoryState);
            var hullPaintingRobot = new HullPaintingRobot(intcode);

            hullPaintingRobot.Paint();

            var panels = hullPaintingRobot.Panels;

            int panelsPainted = panels
                                .Select(x => x.Location)
                                .Distinct()
                                .Count();

            Assert.Equal(2336, panelsPainted);
        }
示例#11
0
        static void Main(string[] args)
        {
            var instructions = "3,8,1005,8,330,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,29,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,101,0,8,51,1,1103,2,10,1006,0,94,1006,0,11,1,1106,13,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1001,8,0,87,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,109,2,1105,5,10,2,103,16,10,1,1103,12,10,2,105,2,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1001,8,0,146,1006,0,49,2,1,12,10,2,1006,6,10,1,1101,4,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,183,1,6,9,10,1006,0,32,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,213,2,1101,9,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,239,1006,0,47,1006,0,4,2,6,0,10,1006,0,58,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,274,2,1005,14,10,1006,0,17,1,104,20,10,1006,0,28,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,309,101,1,9,9,1007,9,928,10,1005,10,15,99,109,652,104,0,104,1,21101,0,937263411860,1,21102,347,1,0,1105,1,451,21101,932440724376,0,1,21102,1,358,0,1105,1,451,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,29015167015,1,21101,0,405,0,1106,0,451,21102,1,3422723163,1,21101,0,416,0,1106,0,451,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,868389376360,1,21101,0,439,0,1105,1,451,21102,825544712960,1,1,21102,1,450,0,1106,0,451,99,109,2,21201,-1,0,1,21101,0,40,2,21102,482,1,3,21102,1,472,0,1106,0,515,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,477,478,493,4,0,1001,477,1,477,108,4,477,10,1006,10,509,1101,0,0,477,109,-2,2106,0,0,0,109,4,2101,0,-1,514,1207,-3,0,10,1006,10,532,21102,1,0,-3,22101,0,-3,1,22102,1,-2,2,21102,1,1,3,21101,551,0,0,1106,0,556,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,579,2207,-4,-2,10,1006,10,579,22102,1,-4,-4,1106,0,647,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,1,598,0,1106,0,556,22101,0,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,617,21102,0,1,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,639,21201,-1,0,1,21102,639,1,0,105,1,514,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0";
            var icc          = new IntCodeComputer(instructions);
            var bot          = new HullPaintingRobot();

            while (icc.State != "HALTED")
            {
                icc.RunIntcode(bot.GetColorCodeOfLocation());
                bot.PaintLocation((int)icc.Outputs.TakeLast(2).First());
                bot.Turn(icc.Outputs.Last());
                bot.Move();
            }

            System.Console.WriteLine(bot.environment.Count);
            var result = bot.environment;

            System.Console.WriteLine(result.Max(kvp => kvp.Key.Item1));
            System.Console.WriteLine(result.Max(kvp => kvp.Key.Item2));
            System.Console.WriteLine(result.Max(kvp => - kvp.Key.Item1));
            System.Console.WriteLine(result.Max(kvp => - kvp.Key.Item2));

            var rows = new char[6][];

            for (int y = -5; y <= 0; y++)
            {
                var row = new char[43];
                for (int x = 0; x < 43; x++)
                {
                    row[x] = bot.GetColorOfLocation((x, y));
                }
                rows[y + 5] = row;
            }

            System.Console.WriteLine(string.Join('\n', rows.Select(row => new string(row))));
        }
示例#12
0
    InputAnswer GetPuzzleData(int part, string name)
    {
        var result = part switch
        {
            1 => new InputAnswer(
                InputHelper.LoadInputFile(DAY, name).ToList(),
                expectedAnswer1: InputHelper.LoadAnswerFile(DAY, part, name)?.FirstOrDefault()?.ToInt32()
                ),
            2 => new InputAnswer(
                InputHelper.LoadInputFile(DAY, name).ToList(),
                expectedAnswer2: InputHelper.LoadAnswerFile(DAY, part, name)
                ),
            _ => throw new ApplicationException($"Invalid part ({part}) value")
        };

        return(result);
    }

    string RunPart1(InputAnswer puzzleData)
    {
        var hull  = new Dictionary <Point, HullColor>();
        var robot = new HullPaintingRobot(puzzleData.Code);

        robot.ScanHull  += (s, e) => e.HullColor = GetHullColor(hull, e.Location);
        robot.PaintHull += (s, e) => SetHullColor(hull, e.Location, e.HullColor);

        robot.Start();
        var result = hull.Count();

        return(Helper.GetPuzzleResultText($"Hull panels painted: {result}", result, puzzleData.ExpectedAnswer));
    }

    string RunPart2(InputAnswer puzzleData)
    {
        var hull  = new Dictionary <Point, HullColor>();
        var robot = new HullPaintingRobot(puzzleData.Code);

        SetHullColor(hull, robot.CurrentLocation, HullColor.White);

        robot.ScanHull  += (s, e) => e.HullColor = GetHullColor(hull, e.Location);
        robot.PaintHull += (s, e) => SetHullColor(hull, e.Location, e.HullColor);

        robot.Start();
        var compositeImage = Helper.DrawPointGrid2D(hull, DrawPanel);//.TrimEnd();


        return(Environment.NewLine + Helper.GetPuzzleResultText(compositeImage, compositeImage, puzzleData.ExpectedAnswer2));
    }

    HullColor GetHullColor(IDictionary <Point, HullColor> hull, Point location)
    {
        if (!hull.ContainsKey(location))
        {
            hull.Add(location, HullColor.Black);
        }
        return(hull[location]);
    }

    void SetHullColor(IDictionary <Point, HullColor> hull, Point location, HullColor color)
    {
        if (hull.ContainsKey(location))
        {
            hull[location] = color;
        }
        else
        {
            hull.Add(location, color);
        }
    }

    string DrawPanel(HullColor panelColor)
    {
        var result = panelColor switch
        {
            HullColor.White => "#",
            _ => " "
        };

        return(result);
    }
示例#13
0
        public void SolvePart2()
        {
            var input = System.IO.File.ReadAllText("../../../input/day_11.txt");

            long[] initialMemoryState = input
                                        .Split(',')
                                        .Select(x => long.Parse(x))
                                        .ToArray();

            var intcode           = new Intcode(initialMemoryState);
            var hullPaintingRobot = new HullPaintingRobot(intcode);

            hullPaintingRobot.Paint();

            var panels = hullPaintingRobot.Panels;

            var allVisitedPoints = panels
                                   .Select(x => x.Location)
                                   .Distinct();

            List <Point> whitePoints = new List <Point>();

            foreach (var p in allVisitedPoints)
            {
                var latestColor = panels
                                  .Where(x => x.Location.Equals(p))
                                  .First()
                                  .Color;

                if (latestColor == 1)
                {
                    whitePoints.Add(p);
                }
            }

            var minX = whitePoints.Min(p => p.x);
            var maxX = whitePoints.Max(p => p.x);
            var minY = whitePoints.Min(p => p.y);
            var maxY = whitePoints.Max(p => p.y);

            var output = new List <string>();

            for (int y = minY; y <= maxY; y++)
            {
                string line = "";
                for (int x = minX; x <= maxX; x++)
                {
                    if (whitePoints.Contains(new Point(x, y)))
                    {
                        line += "#";
                        Console.Write("#");
                    }
                    else
                    {
                        line += " ";
                        Console.Write(" ");
                    }
                }

                output.Add(line);
                Console.Write("\n");
            }

            var expected = new string[]
            {
                "##  # ####  ##  #### #  # ###  #    ### ",
                " #  #    # #  # #    # #  #  # #    #  #",
                " #  #   #  #  # ###  ##   ###  #    #  #",
                " #  #  #   #### #    # #  #  # #    ### ",
                " #  # #    #  # #    # #  #  # #    #   ",
                "  ##  #### #  # #### #  # ###  #### #   "
            };

            Assert.Equal(expected, output.ToArray());
        }