Exemple #1
0
        public async Task Run(RobotInstruction instruction)
        {
            Debug.Assert(this.RobotId == instruction.RobotId);
            await itemEvent.WaitAsync();

            Interlocked.Increment(ref RunningBots);
            SortedSet <int> items = new SortedSet <int>();

            for (int i = 0; i < 2; i++)
            {
                int  item;
                bool hasItem = this.heldMicrochips.TryDequeue(out item);
                Debug.Assert(hasItem);
                items.Add(item);
            }

            Debug.Assert(items.Count == 2);

            int low  = items.First();
            int high = items.Last();

            TransferItem(low, instruction.LowTarget, instruction.LowTargetIsOutput);
            TransferItem(high, instruction.HighTarget, instruction.HighTargetIsOutput);
            Interlocked.Decrement(ref RunningBots);
        }
Exemple #2
0
 public void QueueInstruction(RobotInstruction instruction)
 {
     // Console.WriteLine("{0} queued instruction", this.RobotId);
     if (this.InstructionTaskQueue == null)
     {
         this.InstructionTaskQueue = Run(instruction);
     }
     else
     {
         this.InstructionTaskQueue = this.InstructionTaskQueue.ContinueWith(_ => Run(instruction));
     }
 }
Exemple #3
0
        private static async Task Day10()
        {
            Dictionary <int, Robot> robots  = new Dictionary <int, Robot>();
            Dictionary <int, Robot> outputs = new Dictionary <int, Robot>();

            for (int i = 0; i < 210; i++)
            {
                robots.Add(i, new Robot(i, robots, outputs));
            }

            for (int i = 0; i < 21; i++)
            {
                outputs.Add(i, new Robot(i, robots, outputs));
            }

            Regex itemParser = new Regex(@"value (?<Item>\d+) goes to bot (?<Target>\d+)");

            foreach (var line in Resources.Day10.Split('\n'))
            {
                RobotInstruction instruction;
                Match            itemInstruction = itemParser.Match(line);
                if (itemInstruction.Success)
                {
                    int item   = int.Parse(itemInstruction.Groups["Item"].Value);
                    int target = int.Parse(itemInstruction.Groups["Target"].Value);
                    robots[target].GiveValue(item);
                }
                else if (RobotInstruction.TryParse(line, out instruction))
                {
                    robots[instruction.RobotId].QueueInstruction(instruction);
                }
                else if (line == "")
                {
                    break;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }

            while (Interlocked.Read(ref Robot.RunningBots) > 0)
            {
                await Task.Delay(100);
            }
        }
Exemple #4
0
        internal static bool TryParse(string input, out RobotInstruction instruction)
        {
            Match match = parser.Match(input);

            if (!match.Success)
            {
                instruction = new RobotInstruction();
                return(false);
            }

            instruction = new RobotInstruction
            {
                RobotId            = int.Parse(match.Groups["RobotId"].Value),
                LowTarget          = int.Parse(match.Groups["LowTarget"].Value),
                LowTargetIsOutput  = match.Groups["LowType"].Value == "output",
                HighTarget         = int.Parse(match.Groups["HighTarget"].Value),
                HighTargetIsOutput = match.Groups["HighType"].Value == "output"
            };

            return(true);
        }