Exemple #1
0
 private static async Task FollowPath(painter.SynchronousIntMachine intMachine, IEnumerable <string> path)
 {
     foreach (var direction in path)
     {
         _ = await RunMachineAsync(intMachine, direction);
     }
 }
Exemple #2
0
        public static async Task Run()
        {
            var inputs = await File.ReadAllTextAsync("inputs\\9.txt");

            painter.SynchronousIntMachine vm = new painter.SynchronousIntMachine(inputs);

            vm.InputQueue.Enqueue(1);

            var c = 0;

            while (vm.RunUntilBlockOrComplete() != painter.SynchronousIntMachine.ReturnCode.Completed)
            {
                c++;
            }

            Console.WriteLine("Part 1: " + vm.OutputQueue.Last());

            vm = new painter.SynchronousIntMachine(inputs);

            vm.InputQueue.Enqueue(2);

            c = 0;

            while (vm.RunUntilBlockOrComplete() != painter.SynchronousIntMachine.ReturnCode.Completed)
            {
                c++;
            }

            Console.WriteLine("Part 2: " + vm.OutputQueue.Last());
        }
Exemple #3
0
        public static async Task <string> Part1Async(string input)
        {
            var intMachine = new painter.SynchronousIntMachine(input);

            var(rooms, collectedItems) = await DiscoverRoomsAndGatherItemsAsync(intMachine);

            var hullBreachRoom         = rooms [HullBreach];
            var securityCheckpointRoom = rooms [SecurityCheckpoint];
            var pathToSecurityCheck    = GetPath(rooms, hullBreachRoom, securityCheckpointRoom);

            await FollowPath(intMachine, pathToSecurityCheck);

            var nextDirection = securityCheckpointRoom.Doors.Keys.Single(d => d != InverseDirections [pathToSecurityCheck.Last()]);

            var inventories = collectedItems
                              .Subsets()
                              .Select(x => x.OrderBy(y => y).ToList())
                              .Append(new List <string> ())
                              .OrderBy(x => Math.Abs(collectedItems.Count() / 2 - x.Count))
                              .ToList();
            var currentInventory = inventories.First();

            string password = null;

            foreach (var inv in inventories)
            {
                if (!currentInventory.SequenceEqual(inv))
                {
                    var stuffToDrop = currentInventory.Except(inv).ToList();
                    var stuffToTake = inv.Except(currentInventory).ToList();
                    foreach (var stuff in stuffToDrop)
                    {
                        _ = await RunMachineAsync(intMachine, string.Concat("drop ", stuff));
                    }
                    foreach (var stuff in stuffToTake)
                    {
                        _ = await RunMachineAsync(intMachine, string.Concat("take ", stuff));
                    }
                }
                currentInventory = inv;

                var response = await RunMachineAsync(intMachine, nextDirection);

                var roomNames = RoomNameRegex.Matches(response).OfType <Match> ().Select(x => x.Groups [1].Value).ToList();
                if (roomNames.Count == 1)
                {
                    password = PasswordRegex.Match(response).Value;
                    break;
                }
            }

            return(password);
        }