1
1
import java.io.File
2
+ import java.util.*
3
+ import kotlin.collections.ArrayList
2
4
3
5
fun main (args : Array <String >) {
4
6
println (" AOC 2023, Day 8, Part 2 starting!!!!" )
@@ -24,11 +26,21 @@ fun main(args: Array<String>) {
24
26
val nodesEndWithZ = findNodesEndsWithZ(allNodes)
25
27
println (" Finding nodes that end with Z: $nodesEndWithZ " )
26
28
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 " )
28
37
29
- val stepsRecursive = traverseNodesIterative(directions, allNodes, nodesEndWithA)
38
+ var stepsStack = foundCount.pop()
39
+ while (! foundCount.empty()) {
40
+ stepsStack = lcm(stepsStack, foundCount.pop())
41
+ }
30
42
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 " )
32
44
33
45
println (" AOC 2023, Day 8, Part 2 completed!!!" )
34
46
}
@@ -55,41 +67,25 @@ fun findNodesEndsWithZ(nodes: ArrayList<Node>):ArrayList<Node> {
55
67
return nodesEndsWithZ
56
68
}
57
69
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 {
63
71
var totalSteps = 0L
64
72
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' ) {
70
76
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 }!!
76
79
} 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 }!!
81
81
}
82
- currentNodes = ArrayList (nextNodes)
82
+ currentNode = nextNode
83
83
84
84
nextStep++
85
85
totalSteps++
86
86
if (nextStep >= directions.length) {
87
87
nextStep = 0
88
88
}
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
- }
93
89
}
94
90
95
91
return totalSteps
0 commit comments