| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- 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<int, Bot>();
- var instructions = new Dictionary<Bot, ((int, Bot low), (int, Bot high))>();
- 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<int, Bot> bots, Dictionary<Bot, ((int, Bot) low, (int, Bot high))> instructions)
- {
- var valueReg = new Regex(@"value (?<m>\d+) goes to bot (?<b>\d+)");
- var otherReg = new Regex(@"bot (?<b>\d+) gives low to (?<lt>output|bot) (?<lb>\d+) and high to (?<ht>output|bot) (?<hb>\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<int, Bot> bots, int botId)
- {
- Bot bot;
- if (bots.ContainsKey(botId)) bot = bots[botId];
- else
- {
- bot = new Bot(botId);
- bots.Add(botId, bot);
- }
- return bot;
- }
- }
- }
|