Equd

AdventOfCode Day 18 2017

Dec 19th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.52 KB | None | 0 0
  1.    public class Duet
  2.     {
  3.         private Dictionary<char, long> dictionary = new Dictionary<char, long>();
  4.  
  5.         private BlockingCollection<long> que = new BlockingCollection<long>();
  6.  
  7.         private long lastSound = 0;
  8.  
  9.         public long this[string i]
  10.         {
  11.             get
  12.             {
  13.                 if (char.IsDigit(i.Last()))
  14.                 {
  15.                     return int.Parse(i);
  16.                 }
  17.                 else
  18.                 {
  19.                     if (i.Length > 1) throw new NotImplementedException();
  20.  
  21.                     if (!dictionary.ContainsKey(i[0]))
  22.                         this.dictionary.Add(i[0], 0);
  23.  
  24.                     return this.dictionary[i[0]];
  25.                 }
  26.             }
  27.             set
  28.             {
  29.                 if (i.Length > 1) throw new NotImplementedException();
  30.                 if (!dictionary.ContainsKey(i[0]))
  31.                     this.dictionary.Add(i[0], 0);
  32.  
  33.                 this.dictionary[i[0]] = value;
  34.             }
  35.         }
  36.  
  37.         public void Solve()
  38.         {
  39.             var cmds = Properties.Resources.TextFile1.Split('\n').Select(x => x.Trim().Split(' ')).ToArray(); ;
  40.             var answerA = Commands(cmds);
  41.  
  42.             var d1 = new Duet();
  43.             d1["p"] = d1["0"];
  44.  
  45.             var d2 = new Duet();
  46.             d2["p"] = d2["1"];
  47.  
  48.             var t1 = new Task<long>(() => d1.Commands2(cmds.ToArray(), d2));
  49.             var t2 = new Task<long>(() => d2.Commands2(cmds.ToArray(), d1));
  50.  
  51.             t1.Start();
  52.             t2.Start();
  53.  
  54.             Task.WaitAll(t1, t2);
  55.  
  56.             var answerB = t2.Result;
  57.         }
  58.  
  59.         public long Commands(string[][] cmds)
  60.         {
  61.             for (int i = 0; i < cmds.Length;)
  62.             {
  63.                 var split = cmds[i];
  64.                 switch (split[0])
  65.                 {
  66.                     case "snd":
  67.                         lastSound = this[split[1]];
  68.                         break;
  69.                     case "set":
  70.                         this[split[1]] = this[split[2]];
  71.                         break;
  72.                     case "add":
  73.                         this[split[1]] += this[split[2]];
  74.                         break;
  75.                     case "mul":
  76.                         this[split[1]] *= this[split[2]];
  77.                         break;
  78.                     case "mod":
  79.                         this[split[1]] %= this[split[2]];
  80.                         break;
  81.                     case "rcv":
  82.                         if (this[split[1]] != 0)
  83.                             return lastSound;
  84.                         break;
  85.                     case "jgz":
  86.                         if(split[1] == "1")
  87.                         { }
  88.                         if (this[split[1]] > 0)
  89.                         {
  90.                             i += int.Parse(split[2]);
  91.                             continue;
  92.                         }
  93.                         break;
  94.                 }
  95.                 i++;
  96.             }
  97.             throw new NotImplementedException();
  98.         }
  99.        
  100.         public long Commands2(string[][] cmds, Duet partner)
  101.         {
  102.             long sndCount = 0;
  103.  
  104.             for (long i = 0; i < cmds.Length;)
  105.             {
  106.                 var split = cmds[i];
  107.                 switch (split[0])
  108.                 {
  109.                     case "snd": partner.que.Add(this[split[1]]); sndCount++; break;
  110.                     case "set": this[split[1]]  = this[split[2]]; break;
  111.                     case "add": this[split[1]] += this[split[2]]; break;
  112.                     case "mul": this[split[1]] *= this[split[2]]; break;
  113.                     case "mod": this[split[1]] %= this[split[2]]; break;
  114.                     case "rcv":
  115.                         if (que.TryTake(out long tmp, 100))
  116.                         {
  117.                             this[split[1]] = tmp;
  118.                         }
  119.                         else
  120.                         {
  121.                             partner.que.CompleteAdding();
  122.                             return sndCount;
  123.                         }                                                
  124.                         break;
  125.                     case "jgz":                        
  126.                         if (this[split[1]] > 0)
  127.                         {
  128.                             i += this[split[2]];
  129.                             continue;
  130.                         }
  131.                         break;
  132.                 }
  133.                 i++;
  134.             }
  135.             throw new NotImplementedException();
  136.         }
Advertisement
Add Comment
Please, Sign In to add comment