diff --git a/AdventOfCode/AOC.Tests/Y2023/Day08.cs b/AdventOfCode/AOC.Tests/Y2023/Day08.cs index f57e78a..e6ac32c 100644 --- a/AdventOfCode/AOC.Tests/Y2023/Day08.cs +++ b/AdventOfCode/AOC.Tests/Y2023/Day08.cs @@ -28,31 +28,135 @@ ZZZ = (ZZZ, ZZZ)", 2)] AAA = (BBB, BBB) BBB = (AAA, ZZZ) ZZZ = (ZZZ, ZZZ)", 6)] - [TestCase(null, 232)] // The actual answer + [TestCase(null, 19199)] // The actual answer public void Part1(string input, int? expected) { string[] lines = input != null ? input.Split("\n") : realData; - //if (expected != null) - //{ - // Assert.That(result, Is.EqualTo(expected.Value)); - //} + // The first line is a looping list of instructions + List instructions = lines[0].ToCharArray().ToList(); - //Console.WriteLine($"Part 1: {result}"); + // Every other line (except the second blank line) takes the form "AAA = (BBB, CCC)" where AAA, BBB, and CCC are all strings representing locations, AAA is the current location, and BBB and CCC are the two possible next locations, BBB if the user moves L and CCC if the user moves R + Dictionary locations = new(); + foreach (string line in lines.Skip(2)) + { + string[] parts = line.Split(" = "); + Console.WriteLine($"{parts[0]}, {parts[1]}"); + string location = parts[0]; + string[] nextLocations = parts[1].Trim('(', ')').Split(", "); + locations.Add(location, (nextLocations[0], nextLocations[1])); + } + + // The user starts at the first location and follows the instructions, looping until they reach location ZZZ + string currentLocation = "AAA"; + int instructionIndex = 0; + int stepsCounter = 0; + while (currentLocation != "ZZZ") + { + char instruction = instructions[instructionIndex]; + (string left, string right) = locations[currentLocation]; + currentLocation = instruction switch + { + 'L' => left, + 'R' => right, + _ => throw new Exception($"Unknown instruction: {instruction}") + }; + + // If we are already at the last instructionIndex, start again from 0, otherwise move to the next index + instructionIndex = instructionIndex == instructions.Count - 1 ? 0 : instructionIndex + 1; + + stepsCounter++; + } + + int result = stepsCounter; + + if (expected != null) + { + Assert.That(result, Is.EqualTo(expected.Value)); + } + + Console.WriteLine($"Part 1: {result}"); } - [TestCase("blah", 1)] + [TestCase(@"LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX)", 6)] [TestCase(null, 1783)] // The actual answer - public void Part2(string input, int? expected) + public async Task Part2(string input, int? expected) { - //string[] lines = input != null ? new[] { input } : realData; - // string[] lines = input != null ? input.Split("\n") : realData; + string[] lines = input != null ? input.Split("\n") : realData; - //if (expected != null) - //{ - // Assert.That(result, Is.EqualTo(expected.Value)); - //} + // The first line is a looping list of instructions + List instructions = lines[0].ToCharArray().ToList(); - //Console.WriteLine($"Part 2: {result}"); + // Every other line (except the second blank line) takes the form "11A = (11B, XXX)" where A11A, 11B, and XXX are all strings representing locations, 11A is the current location, and 11B and XXX are the two possible next locations, 11B if the user moves L and XXX if the user moves R + Dictionary locations = new(); + foreach (string line in lines.Skip(2)) + { + string[] parts = line.Split(" = "); + string location = parts[0]; + string[] nextLocations = parts[1].Trim('(', ')').Split(", "); + locations.Add(location, (nextLocations[0], nextLocations[1])); + } + + // The user simultaneously starts at all locations that end with "A" and follow all of the paths at the same time until they all simultaneously end up at nodes that end with "Z" + List currentLocations = locations.Keys.Where(k => k.EndsWith("A")).ToList(); + + // Print out all the starting locations from the currentLocations list as a single string comma separated + Console.WriteLine($"Starting locations: {string.Join(", ", currentLocations)}"); + + int instructionIndex = 0; + int stepsCounter = 0; + while (currentLocations.Any(l => !l.EndsWith("Z"))) + { + // Sort in parallel for large lists + Task[] tasks = new Task[currentLocations.Count]; + + // For each location, follow the instruction + for (int i = 0; i < currentLocations.Count; i++) + { + int localI = i; // Local copy of the loop variable + char instruction = instructions[instructionIndex]; + tasks[localI] = Task.Factory.StartNew(() => + { + (string left, string right) = locations[currentLocations[localI]]; + string newLocation = instruction switch + { + 'L' => left, + 'R' => right, + _ => throw new Exception($"Unknown instruction: {instruction}") + }; + + // Commented out due to ballooning memory + // Console.WriteLine($"Moved {instruction} from {currentLocations[localI]} to {newLocation}"); + + // Update the current location + currentLocations[localI] = newLocation; + }); + } + + await Task.WhenAll(tasks); + + // If we are already at the last instructionIndex, start again from 0, otherwise move to the next index + instructionIndex = instructionIndex == instructions.Count - 1 ? 0 : instructionIndex + 1; + + stepsCounter++; + } + + int result = stepsCounter; + + if (expected != null) + { + Assert.That(result, Is.EqualTo(expected.Value)); + } + + Console.WriteLine($"Part 2: {result}"); } } diff --git a/AdventOfCode/AOC.Tests/Y2023/Tasks/Day08.txt b/AdventOfCode/AOC.Tests/Y2023/Tasks/Day08.txt index 4e341ae..554b01c 100644 --- a/AdventOfCode/AOC.Tests/Y2023/Tasks/Day08.txt +++ b/AdventOfCode/AOC.Tests/Y2023/Tasks/Day08.txt @@ -29,3 +29,34 @@ BBB = (AAA, ZZZ) ZZZ = (ZZZ, ZZZ) Starting at AAA, follow the left/right instructions. How many steps are required to reach ZZZ? +--- Part Two --- +The sandstorm is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take significantly more steps to escape! + +What if the map isn't for people - what if the map is for ghosts? Are ghosts even bound by the laws of spacetime? Only one way to find out. + +After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z! If you were a ghost, you'd probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z. + +For example: + +LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX) +Here, there are two starting nodes, 11A and 22A (because they both end with A). As you follow each left/right instruction, use that instruction to simultaneously navigate away from both nodes you're currently on. Repeat this process until all of the nodes you're currently on end with Z. (If only some of the nodes you're on end with Z, they act like any other node and you continue as normal.) In this example, you would proceed as follows: + +Step 0: You are at 11A and 22A. +Step 1: You choose all of the left paths, leading you to 11B and 22B. +Step 2: You choose all of the right paths, leading you to 11Z and 22C. +Step 3: You choose all of the left paths, leading you to 11B and 22Z. +Step 4: You choose all of the right paths, leading you to 11Z and 22B. +Step 5: You choose all of the left paths, leading you to 11B and 22C. +Step 6: You choose all of the right paths, leading you to 11Z and 22Z. +So, in this example, you end up entirely on nodes that end in Z after 6 steps. + +Simultaneously start on every node that ends with A. How many steps does it take before you're only on nodes that end with Z? \ No newline at end of file