Program.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using D19._1;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace D21._1
  6. {
  7. class ManagedComputer : Computer
  8. {
  9. public new uint Ip => base.Ip;
  10. public uint GetRegister(int id) => CPU.Registers[id];
  11. public void RefreshRegisters(uint value)
  12. {
  13. CPU.RefreshRegisters();
  14. CPU.Registers[0] = value;
  15. }
  16. }
  17. class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. if (args.Length < 1) return;
  22. if (File.Exists(args[0]) == false) return;
  23. var pc = new ManagedComputer();
  24. var file = File.OpenText(args[0]);
  25. pc.LoadProgram(file);
  26. file.Close();
  27. pc.DumpRegisters();
  28. Console.WriteLine("==== EXECUTING PROGRAM ===");
  29. bool continu = true;
  30. uint first = 0, last = 0;
  31. HashSet<uint> all = new HashSet<uint>();
  32. for (uint i = 0; continu ; i++)
  33. {
  34. pc.RefreshRegisters(i);
  35. do
  36. {
  37. continu = pc.ExecInstruction();
  38. if (pc.Ip == 29 && first == 0)
  39. first = pc.GetRegister(1);
  40. if (pc.Ip == 29)
  41. {
  42. var l = pc.GetRegister(1);
  43. if (all.Add(l) == true && l < Int32.MaxValue) last = l;
  44. else continu = false;
  45. }
  46. } while (continu);
  47. }
  48. Console.WriteLine("====== PROGRAM ENDED =====");
  49. pc.DumpRegisters();
  50. Console.WriteLine($"Part 1 answer is : {first}");
  51. Console.WriteLine($"Part 2 answer is : {last}");
  52. }
  53. }
  54. }