|
|
@@ -0,0 +1,64 @@
|
|
|
+using D19._1;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+
|
|
|
+namespace D21._1
|
|
|
+{
|
|
|
+ class ManagedComputer : Computer
|
|
|
+ {
|
|
|
+ public new uint Ip => base.Ip;
|
|
|
+ public uint GetRegister(int id) => CPU.Registers[id];
|
|
|
+ public void RefreshRegisters(uint value)
|
|
|
+ {
|
|
|
+ CPU.RefreshRegisters();
|
|
|
+ CPU.Registers[0] = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class Program
|
|
|
+ {
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ if (args.Length < 1) return;
|
|
|
+ if (File.Exists(args[0]) == false) return;
|
|
|
+
|
|
|
+ var pc = new ManagedComputer();
|
|
|
+ var file = File.OpenText(args[0]);
|
|
|
+ pc.LoadProgram(file);
|
|
|
+ file.Close();
|
|
|
+
|
|
|
+ pc.DumpRegisters();
|
|
|
+
|
|
|
+ Console.WriteLine("==== EXECUTING PROGRAM ===");
|
|
|
+
|
|
|
+ bool continu = true;
|
|
|
+ uint first = 0, last = 0;
|
|
|
+ HashSet<uint> all = new HashSet<uint>();
|
|
|
+ for (uint i = 0; continu ; i++)
|
|
|
+ {
|
|
|
+ pc.RefreshRegisters(i);
|
|
|
+
|
|
|
+ do
|
|
|
+ {
|
|
|
+ continu = pc.ExecInstruction();
|
|
|
+
|
|
|
+ if (pc.Ip == 29 && first == 0)
|
|
|
+ first = pc.GetRegister(1);
|
|
|
+ if (pc.Ip == 29)
|
|
|
+ {
|
|
|
+ var l = pc.GetRegister(1);
|
|
|
+ if (all.Add(l) == true && l < Int32.MaxValue) last = l;
|
|
|
+ else continu = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ } while (continu);
|
|
|
+ }
|
|
|
+
|
|
|
+ Console.WriteLine("====== PROGRAM ENDED =====");
|
|
|
+ pc.DumpRegisters();
|
|
|
+ Console.WriteLine($"Part 1 answer is : {first}");
|
|
|
+ Console.WriteLine($"Part 2 answer is : {last}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|