Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
- static async void Day10Second()
- {
- /*string input = "value 5 goes to bot 2\n" +
- "bot 2 gives low to bot 1 and high to bot 0\n" +
- "value 3 goes to bot 1\n" +
- "bot 1 gives low to output 1 and high to bot 0\n" +
- "bot 0 gives low to output 2 and high to output 0\n" +
- "value 2 goes to bot 2";*/
- string input = File.ReadAllText(@"C:\users\Jason\Desktop\day10Input.txt");
- var evaluators = new Dictionary<int, Evaluator>();
- using (var stream = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(input)), Encoding.UTF8))
- {
- while (!stream.EndOfStream)
- {
- string line = await stream.ReadLineAsync();
- if (line.StartsWith("value"))
- {
- string[] tokens = Regex.Split(line, @"value (.*) goes to bot (.*)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
- int val = int.Parse(tokens[0]);
- int bot = int.Parse(tokens[1]);
- Evaluator evaluator;
- if (!evaluators.TryGetValue(bot, out evaluator))
- {
- evaluator = new Evaluator(bot);
- evaluators.Add(bot, evaluator);
- }
- evaluator.AddValueAsync(val);
- }
- else if (line.StartsWith("bot"))
- {
- string[] tokens = Regex.Split(line, @"bot (.*) gives low to (.*) (.*) and high to (.*) (.*)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
- int bot = int.Parse(tokens[0]);
- string lowTargetType = tokens[1];
- int lowTargetValue = int.Parse(tokens[2]);
- string highTargetType = tokens[3];
- int highTargetValue = int.Parse(tokens[4]);
- if (lowTargetType == "output")
- {
- lowTargetValue = (lowTargetValue * -1) - 1;
- }
- if (highTargetType == "output")
- {
- highTargetValue = (highTargetValue * -1) - 1;
- }
- Evaluator thisEvaluator, lowEvaluator, highEvaluator;
- if (!evaluators.TryGetValue(bot, out thisEvaluator))
- {
- thisEvaluator = new Evaluator(bot);
- evaluators.Add(bot, thisEvaluator);
- }
- if (!evaluators.TryGetValue(lowTargetValue, out lowEvaluator))
- {
- lowEvaluator = new Evaluator(lowTargetValue);
- evaluators.Add(lowTargetValue, lowEvaluator);
- }
- if (!evaluators.TryGetValue(highTargetValue, out highEvaluator))
- {
- highEvaluator = new Evaluator(highTargetValue);
- evaluators.Add(highTargetValue, highEvaluator);
- }
- thisEvaluator.LowTarget = lowEvaluator;
- thisEvaluator.HighTarget = highEvaluator;
- }
- }
- }
- tcs.SetResult(true);
- foreach(var kvp in evaluators)
- {
- if(kvp.Key < 0)
- {
- Console.WriteLine($"Output {(kvp.Key + 1) * -1} has values: {kvp.Value.One}, {kvp.Value.Two}");
- }
- }
- }
- class Evaluator
- {
- public int id;
- public Evaluator(int id)
- {
- this.id = id;
- }
- public int? One { get; private set; }
- public int? Two { get; private set; }
- public Evaluator LowTarget;
- public Evaluator HighTarget;
- public async Task AddValueAsync(int val)
- {
- if(this.One == null)
- {
- this.One = val;
- }
- else
- {
- this.Two = val;
- int low = this.GetLow();
- int high = this.GetHigh();
- if(low == 17 && high == 61)
- {
- Console.WriteLine("FOUND IT: " + this.id);
- }
- await tcs.Task;
- await this.LowTarget?.AddValueAsync(low);
- await this.HighTarget?.AddValueAsync(high);
- this.One = null;
- this.Two = null;
- }
- }
- public int GetLow()
- {
- return this.One.Value < this.Two.Value ? this.One.Value : this.Two.Value;
- }
- public int GetHigh()
- {
- return this.One.Value > this.Two.Value ? this.One.Value : this.Two.Value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement