Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Duet
- {
- private Dictionary<char, long> dictionary = new Dictionary<char, long>();
- private BlockingCollection<long> que = new BlockingCollection<long>();
- private long lastSound = 0;
- public long this[string i]
- {
- get
- {
- if (char.IsDigit(i.Last()))
- {
- return int.Parse(i);
- }
- else
- {
- if (i.Length > 1) throw new NotImplementedException();
- if (!dictionary.ContainsKey(i[0]))
- this.dictionary.Add(i[0], 0);
- return this.dictionary[i[0]];
- }
- }
- set
- {
- if (i.Length > 1) throw new NotImplementedException();
- if (!dictionary.ContainsKey(i[0]))
- this.dictionary.Add(i[0], 0);
- this.dictionary[i[0]] = value;
- }
- }
- public void Solve()
- {
- var cmds = Properties.Resources.TextFile1.Split('\n').Select(x => x.Trim().Split(' ')).ToArray(); ;
- var answerA = Commands(cmds);
- var d1 = new Duet();
- d1["p"] = d1["0"];
- var d2 = new Duet();
- d2["p"] = d2["1"];
- var t1 = new Task<long>(() => d1.Commands2(cmds.ToArray(), d2));
- var t2 = new Task<long>(() => d2.Commands2(cmds.ToArray(), d1));
- t1.Start();
- t2.Start();
- Task.WaitAll(t1, t2);
- var answerB = t2.Result;
- }
- public long Commands(string[][] cmds)
- {
- for (int i = 0; i < cmds.Length;)
- {
- var split = cmds[i];
- switch (split[0])
- {
- case "snd":
- lastSound = this[split[1]];
- break;
- case "set":
- this[split[1]] = this[split[2]];
- break;
- case "add":
- this[split[1]] += this[split[2]];
- break;
- case "mul":
- this[split[1]] *= this[split[2]];
- break;
- case "mod":
- this[split[1]] %= this[split[2]];
- break;
- case "rcv":
- if (this[split[1]] != 0)
- return lastSound;
- break;
- case "jgz":
- if(split[1] == "1")
- { }
- if (this[split[1]] > 0)
- {
- i += int.Parse(split[2]);
- continue;
- }
- break;
- }
- i++;
- }
- throw new NotImplementedException();
- }
- public long Commands2(string[][] cmds, Duet partner)
- {
- long sndCount = 0;
- for (long i = 0; i < cmds.Length;)
- {
- var split = cmds[i];
- switch (split[0])
- {
- case "snd": partner.que.Add(this[split[1]]); sndCount++; break;
- case "set": this[split[1]] = this[split[2]]; break;
- case "add": this[split[1]] += this[split[2]]; break;
- case "mul": this[split[1]] *= this[split[2]]; break;
- case "mod": this[split[1]] %= this[split[2]]; break;
- case "rcv":
- if (que.TryTake(out long tmp, 100))
- {
- this[split[1]] = tmp;
- }
- else
- {
- partner.que.CompleteAdding();
- return sndCount;
- }
- break;
- case "jgz":
- if (this[split[1]] > 0)
- {
- i += this[split[2]];
- continue;
- }
- break;
- }
- i++;
- }
- throw new NotImplementedException();
- }
Advertisement
Add Comment
Please, Sign In to add comment