Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. static TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
  2.  
  3.         static async void Day10Second()
  4.         {
  5.             /*string input = "value 5 goes to bot 2\n" +
  6.                           "bot 2 gives low to bot 1 and high to bot 0\n" +
  7.                           "value 3 goes to bot 1\n" +
  8.                           "bot 1 gives low to output 1 and high to bot 0\n" +
  9.                           "bot 0 gives low to output 2 and high to output 0\n" +
  10.                           "value 2 goes to bot 2";*/
  11.  
  12.             string input = File.ReadAllText(@"C:\users\Jason\Desktop\day10Input.txt");
  13.  
  14.             var evaluators = new Dictionary<int, Evaluator>();
  15.             using (var stream = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(input)), Encoding.UTF8))
  16.             {
  17.                 while (!stream.EndOfStream)
  18.                 {
  19.                     string line = await stream.ReadLineAsync();
  20.                    
  21.                     if (line.StartsWith("value"))
  22.                     {
  23.                         string[] tokens = Regex.Split(line, @"value (.*) goes to bot (.*)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
  24.                         int val = int.Parse(tokens[0]);
  25.                         int bot = int.Parse(tokens[1]);
  26.  
  27.                         Evaluator evaluator;
  28.                         if (!evaluators.TryGetValue(bot, out evaluator))
  29.                         {
  30.                             evaluator = new Evaluator(bot);
  31.                             evaluators.Add(bot, evaluator);
  32.                         }
  33.  
  34.                         evaluator.AddValueAsync(val);
  35.                     }
  36.                     else if (line.StartsWith("bot"))
  37.                     {
  38.                         string[] tokens = Regex.Split(line, @"bot (.*) gives low to (.*) (.*) and high to (.*) (.*)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
  39.                         int bot = int.Parse(tokens[0]);
  40.                         string lowTargetType = tokens[1];
  41.                         int lowTargetValue = int.Parse(tokens[2]);
  42.                         string highTargetType = tokens[3];
  43.                         int highTargetValue = int.Parse(tokens[4]);
  44.  
  45.                         if (lowTargetType == "output")
  46.                         {
  47.                             lowTargetValue = (lowTargetValue * -1) - 1;
  48.                         }
  49.                         if (highTargetType == "output")
  50.                         {
  51.                             highTargetValue = (highTargetValue * -1) - 1;
  52.                         }
  53.  
  54.                         Evaluator thisEvaluator, lowEvaluator, highEvaluator;
  55.                         if (!evaluators.TryGetValue(bot, out thisEvaluator))
  56.                         {
  57.                             thisEvaluator = new Evaluator(bot);
  58.                             evaluators.Add(bot, thisEvaluator);
  59.                         }
  60.                         if (!evaluators.TryGetValue(lowTargetValue, out lowEvaluator))
  61.                         {
  62.                             lowEvaluator = new Evaluator(lowTargetValue);
  63.                             evaluators.Add(lowTargetValue, lowEvaluator);
  64.                         }
  65.                         if (!evaluators.TryGetValue(highTargetValue, out highEvaluator))
  66.                         {
  67.                             highEvaluator = new Evaluator(highTargetValue);
  68.                             evaluators.Add(highTargetValue, highEvaluator);
  69.                         }
  70.  
  71.                         thisEvaluator.LowTarget = lowEvaluator;
  72.                         thisEvaluator.HighTarget = highEvaluator;
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             tcs.SetResult(true);
  78.  
  79.             foreach(var kvp in evaluators)
  80.             {                  
  81.                 if(kvp.Key < 0)
  82.                 {
  83.                     Console.WriteLine($"Output {(kvp.Key + 1) * -1} has values: {kvp.Value.One}, {kvp.Value.Two}");
  84.                 }
  85.             }
  86.         }
  87.  
  88.         class Evaluator
  89.         {
  90.             public int id;
  91.  
  92.             public Evaluator(int id)
  93.             {
  94.                 this.id = id;
  95.             }
  96.  
  97.             public int? One { get; private set; }
  98.             public int? Two { get; private set; }
  99.  
  100.             public Evaluator LowTarget;
  101.  
  102.             public Evaluator HighTarget;
  103.  
  104.             public async Task AddValueAsync(int val)
  105.             {
  106.                 if(this.One == null)
  107.                 {
  108.                     this.One = val;
  109.                 }
  110.                 else
  111.                 {
  112.                     this.Two = val;
  113.                     int low = this.GetLow();
  114.                     int high = this.GetHigh();
  115.                     if(low == 17 && high == 61)
  116.                     {
  117.                         Console.WriteLine("FOUND IT: " + this.id);
  118.                     }
  119.                     await tcs.Task;
  120.                     await this.LowTarget?.AddValueAsync(low);
  121.                     await this.HighTarget?.AddValueAsync(high);
  122.  
  123.                     this.One = null;
  124.                     this.Two = null;
  125.                 }
  126.             }
  127.  
  128.             public int GetLow()
  129.             {
  130.                 return this.One.Value < this.Two.Value ? this.One.Value : this.Two.Value;
  131.             }
  132.  
  133.             public int GetHigh()
  134.             {
  135.                 return this.One.Value > this.Two.Value ? this.One.Value : this.Two.Value;
  136.             }
  137.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement