AdventOfCode 2023 Day08 Part2, naïve implementation.
This commit is contained in:
@@ -35,6 +35,41 @@ 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?
|
||||
|
||||
|
||||
*/
|
||||
|
||||
@@ -56,7 +91,18 @@ public class Day08 : IDay
|
||||
|
||||
public string ResolvePart2(string[] inputs)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
Map map = new(inputs);
|
||||
List<MapNode> startNodes = map.Nodes.Where(n => n.Key.EndsWith("A")).ToList();
|
||||
|
||||
List<MapWalker> walkers = startNodes.Select(n => new MapWalker(map, n.Key)).ToList();
|
||||
while (walkers.All(w => w.CurrentNode.Key.EndsWith("Z")) == false)
|
||||
{
|
||||
foreach (MapWalker walker in walkers)
|
||||
{
|
||||
walker.Step();
|
||||
}
|
||||
}
|
||||
return walkers.First().Steps.ToString();
|
||||
}
|
||||
|
||||
private class MapNode
|
||||
@@ -102,9 +148,9 @@ public class Day08 : IDay
|
||||
return _dictMapNodes.GetValueOrDefault(key);
|
||||
}
|
||||
|
||||
public char GetInstruction(int step)
|
||||
public char GetInstruction(long step)
|
||||
{
|
||||
char leftRightInstruction = _strLeftRightInstructions[step % _strLeftRightInstructions.Length];
|
||||
char leftRightInstruction = _strLeftRightInstructions[(int)(step % _strLeftRightInstructions.Length)];
|
||||
return leftRightInstruction;
|
||||
}
|
||||
}
|
||||
@@ -114,7 +160,7 @@ public class Day08 : IDay
|
||||
private readonly Map _map;
|
||||
|
||||
private MapNode _currentNode;
|
||||
private int _steps;
|
||||
private long _steps;
|
||||
|
||||
public MapWalker(Map map, string startKey)
|
||||
{
|
||||
@@ -142,7 +188,7 @@ public class Day08 : IDay
|
||||
get { return _currentNode; }
|
||||
}
|
||||
|
||||
public int Steps
|
||||
public long Steps
|
||||
{
|
||||
get { return _steps; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user