using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace D10._1 { public class Bot { public int ID { get; } private int? M1; private int? M2; public Bot(int id) => ID = id; public bool IsFull() => M1.HasValue && M2.HasValue; public void Take(int m) { if (M1.HasValue == false) M1 = m; else if (M2.HasValue == false) M2 = m; else throw new Exception(); } public int GiveLowest() { if (M1.HasValue == false && M2.HasValue == false) throw new Exception(); if (M1.HasValue == false && M2.HasValue == true || M2 < M1) { int r = M2.Value; M2 = null; return r; } if (M1.HasValue == true && M2.HasValue == false || M1 < M2) { int r = M1.Value; M1 = null; return r; } throw new Exception(); } public int GiveHighest() { if (M1.HasValue == false && M2.HasValue == false) throw new Exception(); if (M1.HasValue == false && M2.HasValue == true || M2 > M1) { int r = M2.Value; M2 = null; return r; } if (M1.HasValue == true && M2.HasValue == false || M1 > M2) { int r = M1.Value; M1 = null; return r; } throw new Exception(); } public override bool Equals(object obj) => obj is Bot bot && ID == bot.ID; public override int GetHashCode() => HashCode.Combine(ID); } public class Program { static void Main(string[] args) { if (args.Length < 1) throw new ArgumentException(); if (File.Exists(args[0]) == false) throw new FileNotFoundException(); var bots = new Dictionary(); var instructions = new Dictionary(); ParseFile(args, bots, instructions); while (true) { var bot = bots.FirstOrDefault(b => b.Value.IsFull()).Value; if (bot == null) break; var lowest = bot.GiveLowest(); var highest = bot.GiveHighest(); if (lowest == 17 && highest == 61) { Console.WriteLine($"The answer is : {bot.ID}"); break; } var ((_, low), (_, high)) = instructions[bot]; low?.Take(lowest); high?.Take(highest); } } public static void ParseFile(string[] args, Dictionary bots, Dictionary instructions) { var valueReg = new Regex(@"value (?\d+) goes to bot (?\d+)"); var otherReg = new Regex(@"bot (?\d+) gives low to (?output|bot) (?\d+) and high to (?output|bot) (?\d+)"); using (var file = File.OpenText(args[0])) { while (true) { var line = file.ReadLine(); if (line == null) break; if (line.StartsWith("value ")) { var r = valueReg.Match(line); var botId = int.Parse(r.Groups["b"].Value); var value = int.Parse(r.Groups["m"].Value); Bot bot = TryAddBot(bots, botId); bot.Take(value); } else { var r = otherReg.Match(line); var botId = int.Parse(r.Groups["b"].Value); var lbotId = int.Parse(r.Groups["lb"].Value); var lType = r.Groups["lt"].Value; var hbotId = int.Parse(r.Groups["hb"].Value); var hType = r.Groups["ht"].Value; ((int, Bot bot) low, (int, Bot bot) high) instr = ((lbotId, null), (hbotId, null)); if (lType == "bot") { Bot lbot = TryAddBot(bots, lbotId); instr.low.bot = lbot; } if (hType == "bot") { Bot hbot = TryAddBot(bots, hbotId); instr.high.bot = hbot; } Bot bot = TryAddBot(bots, botId); instructions.Add(bot, instr); } } } } private static Bot TryAddBot(Dictionary bots, int botId) { Bot bot; if (bots.ContainsKey(botId)) bot = bots[botId]; else { bot = new Bot(botId); bots.Add(botId, bot); } return bot; } } }