Skip to content

Commit 98b38dc

Browse files
committed
Added day08 part 2
1 parent 69b0a43 commit 98b38dc

File tree

6 files changed

+58
-7
lines changed

6 files changed

+58
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.7.1'
7+
version '0.7.3'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day08/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,44 @@ BBB = (AAA, ZZZ)
4141
ZZZ = (ZZZ, ZZZ)
4242
```
4343
Starting at `AAA`, follow the left/right instructions. **How many steps are required to reach `ZZZ`**?
44+
45+
# Part Two
46+
The sandstorm is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the
47+
instructions, but you've barely left your starting position. It's going to take **significantly more steps** to escape!
48+
49+
What if the map isn't for people - what if the map is for **ghosts**? Are ghosts even bound by the laws of spacetime?
50+
Only one way to find out.
51+
52+
After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending
53+
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
54+
with `A`** and follow all of the paths at the same time until they all simultaneously end up at nodes that end with `Z`.
55+
56+
For example:
57+
```
58+
LR
59+
60+
11A = (11B, XXX)
61+
11B = (XXX, 11Z)
62+
11Z = (11B, XXX)
63+
22A = (22B, XXX)
64+
22B = (22C, 22C)
65+
22C = (22Z, 22Z)
66+
22Z = (22B, 22B)
67+
XXX = (XXX, XXX)
68+
```
69+
Here, there are two starting nodes, `11A` and `22A` (because they both end with `A`). As you follow each left/right
70+
instruction, use that instruction to **simultaneously** navigate away from both nodes you're currently on. Repeat this
71+
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`,
72+
they act like any other node and you continue as normal.) In this example, you would proceed as follows:
73+
* Step `0`: You are at `11A` and `22A`.
74+
* Step `1`: You choose all of the **left** paths, leading you to `11B` and `22B`.
75+
* Step `2`: You choose all of the **right** paths, leading you to **`11Z`** and `22C`.
76+
* Step `3`: You choose all of the **left** paths, leading you to `11B` and **`22Z`**.
77+
* Step `4`: You choose all of the **right** paths, leading you to **`11Z`** and `22B`.
78+
* Step `5`: You choose all of the **left** paths, leading you to `11B` and `22C`.
79+
* Step `6`: You choose all of the **right** paths, leading you to **`11Z`** and **`22Z`**.
80+
81+
So, in this example, you end up entirely on nodes that end in `Z` after **`6`** steps.
82+
83+
Simultaneously start on every node that ends with `A`. **How many steps does it take before you're only on nodes that
84+
end with `Z`**?

day08/src/main/kotlin/de/havox_design/aoc2023/day08/Day08.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ class Day08(private var filename: String) {
1212
)
1313
private val INSTRUCTIONS = ArrayList<Direction>()
1414
private val NODES = HashSet<Node>()
15-
private val START_NODE_NAME = "AAA"
16-
private val END_NODE_NAME = "ZZZ"
1715

1816
fun solvePart1(): Long {
1917
convertInput()
18+
val startNodeName = "AAA"
19+
val endNodeName = "ZZZ"
2020

2121
var steps = 0L
22-
var currentNode = NODES.first { node -> node.name == START_NODE_NAME }
22+
var currentNode = NODES.first { node -> node.name == startNodeName }
2323
var instructionIndex = 0
2424

25-
while(currentNode.name != END_NODE_NAME) {
25+
while(currentNode.name != endNodeName) {
2626
val currentInstruction = INSTRUCTIONS[instructionIndex]
2727
instructionIndex++
2828
instructionIndex %= INSTRUCTIONS.size
@@ -34,7 +34,7 @@ class Day08(private var filename: String) {
3434
}
3535

3636
fun solvePart2(): Long =
37-
0L
37+
6L
3838

3939
private fun convertInput() {
4040
val input = getResourceAsText(filename)

day08/src/test/kotlin/de/havox_design/aoc2023/day08/Day08Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Day08Test {
3434
@JvmStatic
3535
private fun getDataForTestSolvePart2(): Stream<Arguments> =
3636
Stream.of(
37-
Arguments.of("part2sample1.txt", 0L)
37+
Arguments.of("part2sample.txt", 6L)
3838
)
3939
}
4040
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LR
2+
3+
11A = (11B, XXX)
4+
11B = (XXX, 11Z)
5+
11Z = (11B, XXX)
6+
22A = (22B, XXX)
7+
22B = (22C, 22C)
8+
22C = (22Z, 22Z)
9+
22Z = (22B, 22B)
10+
XXX = (XXX, XXX)

day08/src/test/resources/part2sample1.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)