public int TryArangementWithFeedback(int input, params int[] amp_settings)
        {
            var memories = Range(0, 5).Select(i => program.ToArray()).ToArray();
            var systems  = Range(0, 5).Select(i => QueueStoreSystem.CreateDuplux()).ToArray();

            foreach (var index in Range(0, 5))
            {
                systems[index].device.Write(amp_settings[index]);
            }

            var results = Range(0, 5).Select(i => new ProgramResult {
                ProgramPointer = 0, State = ProgramState.Running, System = systems[i].system
            }).ToArray();
            int amp   = 0;
            int loops = 0;
            var last  = RunControllerSoftwareWithFeedback(memories[amp], results[amp], systems[amp].device, 0);

            results[amp] = last;
            int[] outputs = new int[] { systems[amp].device.Read(), 0, 0, 0, 0 };
            while (!results.All(r => r.State == ProgramState.Halted))
            {
                int last_output = outputs[amp];
                amp += 1;
                if (amp > 4)
                {
                    (amp, loops) = (0, loops + 1);
                }

                last         = RunControllerSoftwareWithFeedback(memories[amp], results[amp], systems[amp].device, last_output);
                results[amp] = last;
                outputs[amp] = systems[amp].device.Read();
            }
            Log($">> feedback cycle completed in {loops} loops");
            return(outputs[4]);
        }
        private int RunControllerSoftware(int amp, int input)
        {
            var memory = program.ToArray();

            var(system, device) = QueueStoreSystem.CreateDuplux();
            device.Write(amp);
            device.Write(input);
            IntcodeComputer.EvaluateProgram(memory, system);
            return(device.Read());
        }
Beispiel #3
0
        static void Day5()
        {
            WriteLine("--- Day 5: Sunny with a Chance of Asteroids ---");
            var program = "Day5/input.txt".ReadLines().Single().Split(",").Select(int.Parse).ToArray();

            WriteLine("Part 1 - AC Diagnostic");
            var(system, device) = QueueStoreSystem.CreateDuplux();
            device.Write(1);
            var copy = program.ToArray();

            Day5Computer.EvaluateProgram(copy, system);
            WriteLine(string.Join(",", device.Buffer));

            WriteLine("Part 2 - Extend thermal radiators");
            (system, device) = QueueStoreSystem.CreateDuplux();
            device.Write(5);
            copy = program.ToArray();
            Day5Computer.EvaluateProgram(copy, system);
            WriteLine(string.Join(",", device.Buffer));
        }