Day 8: Haunted Wasteland

Completed in ~2 hours, 17 minutes.
9,554แต—สฐ worldwide.
2023-12-08

You're still riding a camel across Desert Island when you spot a sandstorm quickly approaching. When you turn to warn the Elf, she disappears before your eyes! To be fair, she had just finished warning you about ghosts a few minutes ago.

One of the camel's pouches is labeled "maps" - sure enough, it's full of documents (your puzzle input) about how to navigate the desert. At least, you're pretty sure that's what they are; one of the documents contains a list of left/right instructions, and the rest of the documents seem to describe some kind of network of labeled nodes.

It seems like you're meant to use the left/right instructions to navigate the network. Perhaps if you have the camel follow the same instructions, you can escape the haunted wasteland!

After examining the maps for a bit, two nodes stick out: AAA and ZZZ. You feel like AAA is where you are now, and you have to follow the left/right instructions until you reach ZZZ.

This format defines each node of the network individually. For example:

1
2
3
4
5
6
7
8
9
RL AAA = (BBB, CCC) BBB = (DDD, EEE) CCC = (ZZZ, GGG) DDD = (DDD, DDD) EEE = (EEE, EEE) GGG = (GGG, GGG) ZZZ = (ZZZ, ZZZ)

Starting with AAA, you need to look up the next element based on the next left/right instruction in your input. In this example, start with AAA and go right (R) by choosing the right element of AAA, CCC. Then, L means to choose the left element of CCC, ZZZ. By following the left/right instructions, you reach ZZZ in 2 steps.

Of course, you might not find ZZZ right away. If you run out of left/right instructions, repeat the whole sequence of instructions as necessary: RL really means RLRLRLRLRLRLRLRL... and so on. For example, here is a situation that takes 6 steps to reach ZZZ:

1
2
3
4
5
LLR AAA = (BBB, BBB) BBB = (AAA, ZZZ) ZZZ = (ZZZ, ZZZ)

Starting at AAA, follow the left/right instructions.How many steps are required to reach ZZZ?

Brute-force solution of walking through the steps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from pathlib import Path import pytest def runner(instructions: str, document: list[str]) -> int: steps = 0 current_step = "AAA" maps: dict[str, tuple[str, str]] = {} for line in document: start, rest = line.split(" = ") left, right = rest.replace("(", "").replace(")", "").split(", ") maps[start] = (left, right) while current_step != "ZZZ": instruction = instructions[steps % len(instructions)] if instruction == "L": current_step = maps[current_step][0] elif instruction == "R": current_step = maps[current_step][1] else: raise AssertionError steps += 1 return steps @pytest.mark.parametrize( "filename,instructions,output", [ ("example-1.txt", "RL", 2), ( "example-3.txt", "LLRLRLLRRLRLRLLRRLRRRLRRRLRRLRRLRLRLRRRLLRLRRLRLRRRLRLLRRLRLRLLRRRLLRLRRRLRLRRLRRLRLLRRLRRLRLRLRLLRLLRRLRRLRRLRRLRRLRLLRLRLRRRLRRRLRRLRLRLRRLRRRLRLRRRLRLRLRLRRRLRRLRRLRRRLLLLRRLRRLRLRRRLRLRRRLRRLLLLRLRLRRRLRRRLRLRRLLRLRLRRRLRLRLRRRLRLLRRRLRRLRLRLRRRLRLLRRLLRRRLRRRLRRRLRRLRLRLRRRLRRRLRRRLLRRRR", 23147, ), ], ) def test_runner(filename: str, instructions: str, output: int) -> None: with open(Path(__file__).with_name(filename)) as file: result = runner(instructions, file.read().splitlines()) assert result == output

Answer: 23,147

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:

1
2
3
4
5
6
7
8
9
10
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?

Instead of a brute-force solution, I counted steps per cycle for each path. I could then take the least common multiple of these steps per cycle to get the final number of steps required for all these to sync up on the end positions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import math from pathlib import Path import pytest def runner(instructions: str, document: list[str]) -> int: steps: list[str] = [] maps: dict[str, tuple[str, str]] = {} for line in document: start, rest = line.split(" = ") left, right = rest.replace("(", "").replace(")", "").split(", ") maps[start] = (left, right) if start.endswith("A"): steps.append(start) steps_index = 0 cycles = [] while True: for index, previous_step in enumerate(steps): current_step = maps[previous_step] instruction = instructions[steps_index % len(instructions)] if instruction == "L": next_step = current_step[0] elif instruction == "R": next_step = current_step[1] else: raise AssertionError if next_step.endswith("Z"): cycles.append(steps_index + 1) steps[index] = next_step if len(cycles) == len(steps): break steps_index += 1 return math.lcm(*cycles) @pytest.mark.parametrize( "filename,instructions,output", [ ("example-2.txt", "LR", 6), ( "example-3.txt", "LLRLRLLRRLRLRLLRRLRRRLRRRLRRLRRLRLRLRRRLLRLRRLRLRRRLRLLRRLRLRLLRRRLLRLRRRLRLRRLRRLRLLRRLRRLRLRLRLLRLLRRLRRLRRLRRLRRLRLLRLRLRRRLRRRLRRLRLRLRRLRRRLRLRRRLRLRLRLRRRLRRLRRLRRRLLLLRRLRRLRLRRRLRLRRRLRRLLLLRLRLRRRLRRRLRLRRLLRLRLRRRLRLRLRRRLRLLRRRLRRLRLRLRRRLRLLRRLLRRRLRRRLRRRLRRLRLRLRRRLRRRLRRRLLRRRR", 22289513667691, ), ], ) def test_runner(filename: str, instructions: str, output: int) -> None: with open(Path(__file__).with_name(filename)) as file: result = runner(instructions, file.read().splitlines()) assert result == output

Answer: 22,289,513,667,691

DayPart 1 TimePart 1 RankPart 2 TimePart 2 Rank
800:09:412,16402:16:399,554

Part 1 was very easy and the kind of problem I really enjoy!

I first tried the brute-force solution on Part 2. Once it didn't complete within 30 seconds, I knew I'd need to implement cycle counting to get a reasonably-fast answer, but I left the script running anyway just in case it happened to complete eventually. That script didn't complete with >2 hours of runtime ๐Ÿ˜ฌ.

Cycle counting was simple in theory, but in practice, I had a lot of trouble figuring out how to do it properly. I did use custom test cases like I did in Day 7, but even then, it took a really long time to figure out a correct implementation ๐Ÿ˜–.