Exemplo n.º 1
0
        public static void Step1()
        {
            var machine = new IntCodeMachine(Input.ToLongArray());
            var grid    = new InfiniteGrid <Color>(Color.Black);

            var painted = new HashSet <Point>();
            var facing  = new Vector(0, -1);

            machine.Drive(
                inputHandler: () => Convert.ToInt32(grid.CurrentColor),
                outputHandler: outputs =>
            {
                if (outputs.Count != 2)
                {
                    throw new ApplicationException();
                }
                grid.CurrentColor = outputs[0] switch
                {
                    0 => Color.Black,
                    1 => Color.White,
                    _ => throw new ApplicationException()
                };

                painted.Add(grid.CurrentPosition);
                facing = outputs[1] switch
                {
                    0 => facing.TurnLeft(),
                    1 => facing.TurnRight(),
                    _ => throw new ApplicationException()
                };
                grid.Move(facing.dX, facing.dY);
            }
                );
            painted.Count.Should().Be(2088);
        }
Exemplo n.º 2
0
        public static void Step2()
        {
            var machine = new IntCodeMachine(Input.ToLongArray());
            var grid    = new InfiniteGrid <Color>(Color.Black);
            var facing  = new Vector(0, -1);

            grid.CurrentColor = Color.White;

            machine.Drive(
                inputHandler: () => Convert.ToInt32(grid.CurrentColor),
                outputHandler: outputs =>
            {
                if (outputs.Count != 2)
                {
                    throw new ApplicationException();
                }
                grid.CurrentColor = outputs[0] switch
                {
                    0 => Color.Black,
                    1 => Color.White,
                    _ => throw new ApplicationException()
                };
                facing = outputs[1] switch
                {
                    0 => facing.TurnLeft(),
                    1 => facing.TurnRight(),
                    _ => throw new ApplicationException()
                };
                grid.Move(facing.dX, facing.dY);
            }
                );

            grid.Rows().ForEach(row =>
            {
                row.ForEach(color => Console.Write(color == Color.Black ? " " : "*"));
                Console.WriteLine();
            });
        }