Program.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace D12._1
  6. {
  7. public class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int[] Registers = new int[5] { 0, 0, 0, 0, 0 };
  12. Work(args, Registers);
  13. Console.WriteLine($"The answer is : {Registers[0]}");
  14. }
  15. public static void Work(string[] args, int[] Registers)
  16. {
  17. var Ops = new Dictionary<string, Action<bool, int, int>>()
  18. {
  19. { "cpy", (i, a, b) => { Registers[b] = i ? Registers[a] : a; } },
  20. { "inc", (i, a, b) => { Registers[a]++; } },
  21. { "dec", (i, a, b) => { Registers[a]--; } },
  22. { "jnz", (i, a, b) => { Registers[4] += (i ? Registers[a] : a) != 0 ? b - 1 : 0; } }
  23. };
  24. List<string[]> instr = File.ReadAllText(args[0]).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)
  25. .Select(n => n.Split(" ")).ToList();
  26. for (; Registers[4] < instr.Count; Registers[4]++)
  27. {
  28. var i = instr[Registers[4]];
  29. ParseLine(i, out int valuea, out int valueb, out bool isreg);
  30. Ops[i[0]](isreg, valuea, valueb);
  31. }
  32. }
  33. private static void ParseLine(string[] i, out int valuea, out int valueb, out bool isreg)
  34. {
  35. valuea = 0;
  36. valueb = 0;
  37. isreg = false;
  38. if (i[1][0] >= 'a')
  39. {
  40. valuea = i[1][0] - 'a';
  41. isreg = true;
  42. }
  43. else valuea = int.Parse(i[1]);
  44. if (i.Length == 3)
  45. {
  46. if (i[2][0] >= 'a') valueb = i[2][0] - 'a';
  47. else valueb = int.Parse(i[2]);
  48. }
  49. }
  50. }
  51. }