Program.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace D07._1
  6. {
  7. public class Program
  8. {
  9. public interface In { }
  10. public class Wire : In
  11. {
  12. public string Name;
  13. }
  14. public class Signal : In
  15. {
  16. public ushort Value;
  17. }
  18. public class Gate
  19. {
  20. public In L1;
  21. public In L2;
  22. public Wire R;
  23. public string Op;
  24. }
  25. public static Signal TestCircuit(List<Gate> gates, Gate gate)
  26. {
  27. if (gate.Op is null && gate.L1 is Signal s)
  28. return s;
  29. if (gate.L1 is Wire w1)
  30. {
  31. var l1g = gates.Where(g => g.R.Name == w1.Name).First();
  32. gate.L1 = TestCircuit(gates, l1g);
  33. }
  34. if (gate.L2 is Wire w2)
  35. {
  36. var l2g = gates.Where(g => g.R.Name == w2.Name).First();
  37. gate.L2 = TestCircuit(gates, l2g);
  38. }
  39. if (gate.R.Name == "a") return gate.L1 as Signal;
  40. if (gate.R.Name == "b") return new Signal() { Value = 956 };
  41. var result = operators[gate.Op]((gate.L1 as Signal)?.Value, (gate.L2 as Signal).Value);
  42. return new Signal() { Value = result };
  43. }
  44. static Dictionary<string, Func<ushort?, ushort, ushort>> operators = new Dictionary<string, Func<ushort?, ushort, ushort>>
  45. {
  46. { "AND", (a, b) => (ushort)(a & b) },
  47. { "OR", (a, b) => (ushort)(a | b) },
  48. { "NOT", (a, b) => (ushort)(~ b) },
  49. { "LSHIFT", (a, b) => (ushort)(a << b) },
  50. { "RSHIFT", (a, b) => (ushort)(a >> b) }
  51. };
  52. static void Main(string[] args)
  53. {
  54. if (args.Length < 1) throw new ArgumentException();
  55. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  56. Signal answer = TestSignalP1(args[0]);
  57. Console.WriteLine($"The answer is : {answer.Value}");
  58. }
  59. public static Signal TestSignalP1(string file)
  60. {
  61. List<Gate> gates = new List<Gate>();
  62. ParseFile(file, gates);
  63. var last = gates.Where(g => g.R.Name == "a").First();
  64. var answer = TestCircuit(gates, last);
  65. return answer;
  66. }
  67. public static void ParseFile(string f, List<Gate> gates)
  68. {
  69. using (var file = File.OpenText(f))
  70. {
  71. while (true)
  72. {
  73. var line = file.ReadLine();
  74. if (line == null) break;
  75. ParseLine(gates, line);
  76. }
  77. }
  78. }
  79. private static void ParseLine(List<Gate> gates, string line)
  80. {
  81. var sp = line.Split(" -> ");
  82. string lpart = sp[0], rpart = sp[1];
  83. bool hasOp = false;
  84. In L1 = null, L2 = null;
  85. var gate = new Gate();
  86. SearchOperator(operators, line, lpart, ref hasOp, ref L1, ref L2, gate);
  87. if (hasOp == false)
  88. {
  89. if (ushort.TryParse(lpart, out ushort lv))
  90. {
  91. L1 = new Signal() { Value = lv };
  92. }
  93. else
  94. {
  95. L1 = new Wire() { Name = lpart };
  96. }
  97. gate.L1 = L1;
  98. }
  99. var R = new Wire() { Name = rpart };
  100. gate.R = R;
  101. gates.Add(gate);
  102. }
  103. private static void SearchOperator(Dictionary<string, Func<ushort?, ushort, ushort>> operators, string line, string lpart, ref bool hasOp, ref In L1, ref In L2, Gate gate)
  104. {
  105. foreach (var op in operators)
  106. {
  107. if (line.Contains(op.Key) == false) continue;
  108. hasOp = true;
  109. var lop = lpart.Split(op.Key);
  110. var a = lop[0].Trim(' ');
  111. var b = lop[1].Trim(' ');
  112. if (ushort.TryParse(a, out ushort aa))
  113. {
  114. L1 = new Signal() { Value = aa };
  115. }
  116. else if (a != string.Empty)
  117. {
  118. L1 = new Wire() { Name = a };
  119. }
  120. if (ushort.TryParse(b, out ushort bb))
  121. {
  122. L2 = new Signal() { Value = bb };
  123. }
  124. else
  125. {
  126. L2 = new Wire() { Name = b };
  127. }
  128. gate.L1 = L1;
  129. gate.L2 = L2;
  130. gate.Op = op.Key;
  131. break;
  132. }
  133. }
  134. }
  135. }