Skip to content

Commit f0e294e

Browse files
committed
Final configuration and solution to Advent of Code 2023, Day 8 Part 2.
1 parent 8fbea9a commit f0e294e

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

2023/day08_2/src/Main.kt

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import java.io.File
2+
import java.util.*
3+
import kotlin.collections.ArrayList
24

35
fun main(args: Array<String>) {
46
println("AOC 2023, Day 8, Part 2 starting!!!!")
@@ -24,11 +26,21 @@ fun main(args: Array<String>) {
2426
val nodesEndWithZ = findNodesEndsWithZ(allNodes)
2527
println("Finding nodes that end with Z: $nodesEndWithZ")
2628

27-
println("tlarsen,L27: lcm(15, 20) = ${lcm(15, 20)}")
29+
val foundCount = Stack<Long>()
30+
nodesEndWithA.forEach {
31+
println("Starting search starting with $it")
32+
foundCount.push(traverseNodeIterative(directions, allNodes, it))
33+
println("found it! Path took ${foundCount.peek()} steps.")
34+
}
35+
36+
println("Final path count results: $foundCount")
2837

29-
val stepsRecursive = traverseNodesIterative(directions, allNodes, nodesEndWithA)
38+
var stepsStack = foundCount.pop()
39+
while(!foundCount.empty()) {
40+
stepsStack = lcm(stepsStack, foundCount.pop())
41+
}
3042

31-
println("Steps taken to traverse to all nodes end with Z: $stepsRecursive")
43+
println("Steps taken to traverse to all nodes end with Z: $stepsStack")
3244

3345
println("AOC 2023, Day 8, Part 2 completed!!!")
3446
}
@@ -55,41 +67,25 @@ fun findNodesEndsWithZ(nodes: ArrayList<Node>):ArrayList<Node> {
5567
return nodesEndsWithZ
5668
}
5769

58-
fun countNodesEndsWithZ(nodes: ArrayList<Node>):Int {
59-
return nodes.count { it.nodeName.last() == 'Z' }
60-
}
61-
62-
fun traverseNodesIterative(directions: String, nodes: ArrayList<Node>, startNodes: ArrayList<Node>): Long {
70+
fun traverseNodeIterative(directions: String, nodes: ArrayList<Node>, startNode: Node): Long {
6371
var totalSteps = 0L
6472
var nextStep = 0
65-
var currentNodes = ArrayList<Node>(startNodes)
66-
val nextNodes = ArrayList<Node>()
67-
val countA = currentNodes.count()
68-
while(countNodesEndsWithZ(currentNodes) != countA) {
69-
nextNodes.clear()
73+
var currentNode = Node(startNode.nodeName, startNode.connectionLeft, startNode.connectionRight)
74+
var nextNode:Node
75+
while(currentNode.nodeName.last() != 'Z') {
7076
val leftOrRight = directions[nextStep]
71-
if(leftOrRight == 'L') {
72-
currentNodes.forEach { theNode ->
73-
val leftNodeName = theNode.connectionLeft
74-
nextNodes.add(nodes.find { it.nodeName == leftNodeName }!!)
75-
}
77+
nextNode = if(leftOrRight.compareTo('L') == 0) {
78+
nodes.find { it.nodeName == currentNode.connectionLeft }!!
7679
} else {
77-
currentNodes.forEach { theNode ->
78-
val rightNodeName = theNode.connectionRight
79-
nextNodes.add(nodes.find { it.nodeName == rightNodeName }!!)
80-
}
80+
nodes.find { it.nodeName == currentNode.connectionRight }!!
8181
}
82-
currentNodes = ArrayList(nextNodes)
82+
currentNode = nextNode
8383

8484
nextStep++
8585
totalSteps++
8686
if(nextStep >= directions.length) {
8787
nextStep = 0
8888
}
89-
//if(totalSteps.mod(1) == 0) {
90-
if((countNodesEndsWithZ(currentNodes) > 2) || (totalSteps.mod(1000000) == 0)) {
91-
println("tlarsen,L78: $totalSteps : ${countNodesEndsWithZ(currentNodes)} / ${countA} current node = $currentNodes")
92-
}
9389
}
9490

9591
return totalSteps
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)

0 commit comments

Comments
 (0)