static void Main(string[] args) { ICMachine Machine = new ICMachine(); int[] InitialState = ICMachine.ParseFile(); // Apply patches InitialState[1] = 12; InitialState[2] = 2; Machine.LoadState(InitialState); Machine.Execute(0); Console.WriteLine(Machine.ReadAddress(0)); // Part 2, noun/verb search for (int noun = 0; noun < 100; noun++) { for (int verb = 0; verb < 100; verb++) { Machine.LoadState(InitialState); Machine.SetNoun(noun); Machine.SetVerb(verb); Machine.Execute(); if (Machine.ReadAddress(0) == 19690720) { Console.WriteLine(100 * noun + verb); return; } } } }
static void Main(string[] args) { ICMachine VM = new ICMachine("input.txt"); //VM.Trace = true; VM.Execute(); }
static void Main(string[] args) { Dictionary <Point, bool> Painted = new Dictionary <Point, bool>(); Point Location = new Point(0, 0); Point Vector = new Point(0, -1); ICMachine Brain = new ICMachine("input.txt"); Painted[Location] = true; Func <bool> IsPainted = () => Painted.ContainsKey(Location) ? Painted[Location] : false; Brain.InteractiveMode = false; Brain.ExecuteThreaded(); while (!Brain.Running) { System.Threading.Thread.Sleep(10); } while (Brain.Running) { Brain.ProvideInput(IsPainted() ? 1 : 0); Brain.AwaitOutput(2); if (!Brain.Running) { break; } Painted[Location] = Brain.GetOutput() == 1 ? true : false; switch (Brain.GetOutput()) { case 0: Vector = RotatePoint(Vector, new Point(0, 0), -90); break; case 1: Vector = RotatePoint(Vector, new Point(0, 0), 90); break; } Location.X += Vector.X; Location.Y += Vector.Y; } Point BoundsLow = new Point(0, 0); foreach (var Extent in Painted.Keys) { BoundsLow.X = Math.Min(BoundsLow.X, Extent.X); BoundsLow.Y = Math.Min(BoundsLow.Y, Extent.Y); } foreach (var Extent in Painted.Keys) { Console.SetCursorPosition(Extent.X - BoundsLow.X, Extent.Y - BoundsLow.Y); Console.Write(Painted[Extent] ? "#" : "."); } Console.SetCursorPosition(20, 20); }
static void Main(string[] args) { ICMachine Machine = new ICMachine("input.txt"); Machine.Execute(); }
static void Main(string[] args) { ICMachine[] VMs = new ICMachine[] { new ICMachine(), new ICMachine(), new ICMachine(), new ICMachine(), new ICMachine() }; var BaseState = ICMachine.ParseFile(); // Part 1 var Highest = 0; foreach (var Permutation in Permutations(new int[] { 0, 1, 2, 3, 4 })) { var PrevOutput = 0; for (int i = 0; i < 5; i++) { VMs[i].LoadState(BaseState); var Input = VMs[i].Input; Input.Enqueue(Permutation[i]); Input.Enqueue(PrevOutput); VMs[i].Execute(); PrevOutput = (int)VMs[i].Output.Dequeue(); } if (PrevOutput > Highest) { Highest = PrevOutput; } } Console.WriteLine(Highest); // Part 2 Highest = 0; foreach (var Permutation in Permutations(new int[] { 5, 6, 7, 8, 9 })) { Console.Write("Testing permutation: {0} {1} {2} {3} {4} = ", Permutation[0], Permutation[1], Permutation[2], Permutation[3], Permutation[4]); for (int i = 0; i < 5; i++) { VMs[i].LoadState(BaseState); VMs[i].InteractiveMode = false; VMs[i].Input.Enqueue(Permutation[i]); VMs[i].ExecuteThreaded(ThreadName: "VM " + i.ToString()); } while (VMs.Any(x => !x.Running)) // Allow VM threads to initialize { Thread.Sleep(10); } int Output = 0; int Iterations = 0; while (VMs.All(x => x.Running)) { Iterations++; for (int i = 0; i < 5; i++) { VMs[i].ProvideInput(Output); VMs[i].AwaitOutput(); Output = (int)VMs[i].Output.Dequeue(); } } Highest = Math.Max(Highest, Output); Console.WriteLine("{0} ({1} iterations)", Output, Iterations); } Console.WriteLine(); Console.WriteLine(Highest); }