Exemplo n.º 1
0
 public ThrusterAmplifiers(long[] programMemory, long[] phases)
 {
     ProgramMemory = programMemory;
     Phases        = phases;
     a             = new IntCodeMachine(programMemory, new List <long>()
     {
         phases[0]
     }.ToArray());
     b = new IntCodeMachine(programMemory, new List <long>()
     {
         phases[1]
     }.ToArray());
     c = new IntCodeMachine(programMemory, new List <long>()
     {
         phases[2]
     }.ToArray());
     d = new IntCodeMachine(programMemory, new List <long>()
     {
         phases[3]
     }.ToArray());
     e = new IntCodeMachine(programMemory, new List <long>()
     {
         phases[4]
     }.ToArray());
 }
Exemplo n.º 2
0
        private static long InternalStep2(string s)
        {
            return(new long[] { 5, 6, 7, 8, 9 }.Permute()
                   .Max(permutation =>
            {
                var machines = permutation
                               .Select((phase, index) =>
                {
                    var machine = new IntCodeMachine(s.ToLongArray());
                    machine.Run(phase);
                    return machine;
                }).ToArray();
                long tail = 0;
                while (machines.Last().Status != IntCodeStatus.Stopped)
                {
                    machines.ForEach(m =>
                    {
                        m.Run(tail);
                        tail = m.Outputs.Last();
                    });
                }

                machines.Select(it => it.Status).Should().AllBeEquivalentTo(IntCodeStatus.Stopped);
                return tail;
            }));
        }
Exemplo n.º 3
0
 public HullPaintingRobot(long[] programMemory, Color startColor)
 {
     Computer         = new IntCodeMachine(programMemory);
     CurrentLocation  = new Point(250, 250);
     CurrentDirection = Direction.North;
     Hull.PaintLocation(CurrentLocation.X, CurrentLocation.Y, startColor);
 }
Exemplo n.º 4
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.º 5
0
        public static void Step1()
        {
            IntCodeMachine.RunUntilStopped(new long[] { 1002, 5, 3, 5, 99, 33 })
            .Memory[5].Should().Be(99);

            var machine = IntCodeMachine.RunUntilStopped(Input, new long[] { 1 });

            machine.Outputs.Last().Should().Be(7839346);
        }
Exemplo n.º 6
0
        private static long InternalStep1(string str)
        {
            var s = str.ToLongArray();

            return(new long[] { 0, 1, 2, 3, 4 }.Permute().Max(permutation =>
            {
                var amplifier = IntCodeMachine.RunUntilStopped(s, permutation[0], 0);
                amplifier = IntCodeMachine.RunUntilStopped(s, permutation[1], amplifier.Outputs.First());
                amplifier = IntCodeMachine.RunUntilStopped(s, permutation[2], amplifier.Outputs.First());
                amplifier = IntCodeMachine.RunUntilStopped(s, permutation[3], amplifier.Outputs.First());
                amplifier = IntCodeMachine.RunUntilStopped(s, permutation[4], amplifier.Outputs.First());
                return amplifier.Outputs.First();
            }));
        }
Exemplo n.º 7
0
        public static void ExecuteStarTwo(string fileLocation = "PuzzleInput/Day9.txt")
        {
            long[] program = File.ReadAllText(fileLocation).Split(',').Select(long.Parse).ToArray();
            long[] input   = new List <long>()
            {
                2
            }.ToArray();

            IntCodeMachine icm = new IntCodeMachine(program, input);

            icm.Run();

            Logger.LogMessage(LogLevel.ANSWER, "9A: BOOST Coordinates: " + icm.Output);
        }
Exemplo n.º 8
0
        public static void ExecuteStarOne(string fileLocation = "PuzzleInput/Day5.txt")
        {
            string line = File.ReadAllText(fileLocation);

            var input = new long[1] {
                1
            };

            IntCodeMachine machine = new IntCodeMachine(Array.ConvertAll(line.Split(','), long.Parse), input);

            machine.Run();

            Logger.LogMessage(LogLevel.ANSWER, "5A: Machine Output: " + machine.Output);
        }
Exemplo n.º 9
0
        public static void Step1()
        {
            IntCodeMachine.RunUntilStopped(new[]
                                           { 109L, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 })
            .Outputs.Should().BeEquivalentTo(new[]
                                             { 109L, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 });

            IntCodeMachine.RunUntilStopped(new long[] { 1102, 34915192, 34915192, 7, 4, 7, 99, 0 })
            .Outputs.Single().Decimate().Should().HaveCount(16);

            IntCodeMachine.RunUntilStopped(new long[] { 104, 1125899906842624, 99 })
            .Outputs.Single().Should().Be(1125899906842624);

            var result = IntCodeMachine.RunUntilStopped(Input, 1);

            result.Outputs.Single().Should().Be(3989758265L);

            var result2 = IntCodeMachine.RunUntilStopped(Input, 2);

            result2.Outputs.Single().Should().Be(76791L);
        }
Exemplo n.º 10
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();
            });
        }
Exemplo n.º 11
0
        public static void Step2()
        {
            IntCodeMachine.RunUntilStopped(new long[] { 3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8 },
                                           new long[] { 8 }).Outputs.Single().Should().Be(1);
            IntCodeMachine.RunUntilStopped(new long[] { 3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8 },
                                           new long[] { 7 }).Outputs.Single().Should().Be(0);

            var example = new long[]
            {
                3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
                1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104,
                999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99
            };

            IntCodeMachine.RunUntilStopped(example, 6).Outputs.Single().Should().Be(999);
            IntCodeMachine.RunUntilStopped(example, 7).Outputs.Single().Should().Be(999);
            IntCodeMachine.RunUntilStopped(example, 8).Outputs.Single().Should().Be(1000);
            IntCodeMachine.RunUntilStopped(example, 9).Outputs.Single().Should().Be(1001);
            IntCodeMachine.RunUntilStopped(example, 10).Outputs.Single().Should().Be(1001);

            var machine = IntCodeMachine.RunUntilStopped(Input, 5);

            machine.Outputs.Last().Should().Be(447803);
        }
Exemplo n.º 12
0
 public ArcadeCabinet(long[] program, long[] input)
 {
     icm = new IntCodeMachine(program, input);
 }