Skip to content

Commit 867f56b

Browse files
committed
Solved day08 part 2, but calculation is more than 1h
1 parent 98b38dc commit 867f56b

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
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.3'
7+
version '0.7.4'
88

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

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,29 @@ class Day08(private var filename: String) {
3333
return steps
3434
}
3535

36-
fun solvePart2(): Long =
37-
6L
36+
fun solvePart2(): Long {
37+
convertInput()
38+
val startNodeSuffix = "A"
39+
val endNodeSuffix = "Z"
40+
41+
var steps = 0L
42+
var currentNodes = NODES.filter { node -> node.name.endsWith(startNodeSuffix) }
43+
val endNodes = NODES.filter { node -> node.name.endsWith(endNodeSuffix) }
44+
var instructionIndex = 0
45+
46+
while(!endNodes.containsAll(currentNodes)) {
47+
val currentInstruction = INSTRUCTIONS[instructionIndex]
48+
instructionIndex++
49+
instructionIndex %= INSTRUCTIONS.size
50+
currentNodes = currentNodes
51+
.parallelStream()
52+
.map{node -> node.getNode(currentInstruction)}
53+
.toList()
54+
steps++
55+
}
56+
57+
return steps
58+
}
3859

3960
private fun convertInput() {
4061
val input = getResourceAsText(filename)

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.havox_design.aoc2023.day08
22

3-
data class Node(val name: String, var left: Node?, var right: Node?) {
3+
data class Node(val name: String, var left: Node?, var right: Node?): Comparable<Node> {
44
fun setNode(direction: Direction, node: Node) {
55
when(direction) {
66
Direction.LEFT -> left = node
@@ -18,4 +18,20 @@ data class Node(val name: String, var left: Node?, var right: Node?) {
1818
Direction.LEFT -> left!!
1919
Direction.RIGHT -> right!!
2020
}
21+
22+
override fun compareTo(other: Node): Int =
23+
name.compareTo(other.name)
24+
25+
override fun equals(other: Any?): Boolean {
26+
if (this === other) return true
27+
if (javaClass != other?.javaClass) return false
28+
29+
other as Node
30+
31+
return name == other.name
32+
}
33+
34+
override fun hashCode(): Int {
35+
return name.hashCode()
36+
}
2137
}

0 commit comments

Comments
 (0)