Program.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace D10._1
  7. {
  8. public class Bot
  9. {
  10. public int ID { get; }
  11. private int? M1;
  12. private int? M2;
  13. public Bot(int id) => ID = id;
  14. public bool IsFull() => M1.HasValue && M2.HasValue;
  15. public void Take(int m)
  16. {
  17. if (M1.HasValue == false) M1 = m;
  18. else if (M2.HasValue == false) M2 = m;
  19. else throw new Exception();
  20. }
  21. public int GiveLowest()
  22. {
  23. if (M1.HasValue == false && M2.HasValue == false) throw new Exception();
  24. if (M1.HasValue == false && M2.HasValue == true ||
  25. M2 < M1)
  26. {
  27. int r = M2.Value;
  28. M2 = null;
  29. return r;
  30. }
  31. if (M1.HasValue == true && M2.HasValue == false ||
  32. M1 < M2)
  33. {
  34. int r = M1.Value;
  35. M1 = null;
  36. return r;
  37. }
  38. throw new Exception();
  39. }
  40. public int GiveHighest()
  41. {
  42. if (M1.HasValue == false && M2.HasValue == false) throw new Exception();
  43. if (M1.HasValue == false && M2.HasValue == true ||
  44. M2 > M1)
  45. {
  46. int r = M2.Value;
  47. M2 = null;
  48. return r;
  49. }
  50. if (M1.HasValue == true && M2.HasValue == false ||
  51. M1 > M2)
  52. {
  53. int r = M1.Value;
  54. M1 = null;
  55. return r;
  56. }
  57. throw new Exception();
  58. }
  59. public override bool Equals(object obj) => obj is Bot bot && ID == bot.ID;
  60. public override int GetHashCode() => HashCode.Combine(ID);
  61. }
  62. public class Program
  63. {
  64. static void Main(string[] args)
  65. {
  66. if (args.Length < 1) throw new ArgumentException();
  67. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  68. var bots = new Dictionary<int, Bot>();
  69. var instructions = new Dictionary<Bot, ((int, Bot low), (int, Bot high))>();
  70. ParseFile(args, bots, instructions);
  71. while (true)
  72. {
  73. var bot = bots.FirstOrDefault(b => b.Value.IsFull()).Value;
  74. if (bot == null) break;
  75. var lowest = bot.GiveLowest();
  76. var highest = bot.GiveHighest();
  77. if (lowest == 17 && highest == 61)
  78. {
  79. Console.WriteLine($"The answer is : {bot.ID}");
  80. break;
  81. }
  82. var ((_, low), (_, high)) = instructions[bot];
  83. low?.Take(lowest);
  84. high?.Take(highest);
  85. }
  86. }
  87. public static void ParseFile(string[] args, Dictionary<int, Bot> bots, Dictionary<Bot, ((int, Bot) low, (int, Bot high))> instructions)
  88. {
  89. var valueReg = new Regex(@"value (?<m>\d+) goes to bot (?<b>\d+)");
  90. var otherReg = new Regex(@"bot (?<b>\d+) gives low to (?<lt>output|bot) (?<lb>\d+) and high to (?<ht>output|bot) (?<hb>\d+)");
  91. using (var file = File.OpenText(args[0]))
  92. {
  93. while (true)
  94. {
  95. var line = file.ReadLine();
  96. if (line == null) break;
  97. if (line.StartsWith("value "))
  98. {
  99. var r = valueReg.Match(line);
  100. var botId = int.Parse(r.Groups["b"].Value);
  101. var value = int.Parse(r.Groups["m"].Value);
  102. Bot bot = TryAddBot(bots, botId);
  103. bot.Take(value);
  104. }
  105. else
  106. {
  107. var r = otherReg.Match(line);
  108. var botId = int.Parse(r.Groups["b"].Value);
  109. var lbotId = int.Parse(r.Groups["lb"].Value);
  110. var lType = r.Groups["lt"].Value;
  111. var hbotId = int.Parse(r.Groups["hb"].Value);
  112. var hType = r.Groups["ht"].Value;
  113. ((int, Bot bot) low, (int, Bot bot) high) instr = ((lbotId, null), (hbotId, null));
  114. if (lType == "bot")
  115. {
  116. Bot lbot = TryAddBot(bots, lbotId);
  117. instr.low.bot = lbot;
  118. }
  119. if (hType == "bot")
  120. {
  121. Bot hbot = TryAddBot(bots, hbotId);
  122. instr.high.bot = hbot;
  123. }
  124. Bot bot = TryAddBot(bots, botId);
  125. instructions.Add(bot, instr);
  126. }
  127. }
  128. }
  129. }
  130. private static Bot TryAddBot(Dictionary<int, Bot> bots, int botId)
  131. {
  132. Bot bot;
  133. if (bots.ContainsKey(botId)) bot = bots[botId];
  134. else
  135. {
  136. bot = new Bot(botId);
  137. bots.Add(botId, bot);
  138. }
  139. return bot;
  140. }
  141. }
  142. }