Advertisement
Guest User

Untitled

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