Skip to content

Commit 60dc4af

Browse files
authored
Merge pull request #91 from tlarsen0/feature/day08.2-2024-10-14
Feature/day08.2 2024 10 14
2 parents 3e8d6c0 + f0e294e commit 60dc4af

File tree

13 files changed

+930
-0
lines changed

13 files changed

+930
-0
lines changed

2023/day08_2/.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/day08_2/.idea/kotlinc.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/day08_2/.idea/libraries/KotlinJavaRuntime.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/day08_2/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/day08_2/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/day08_2/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/day08_2/day08_2.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
10+
</content>
11+
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
14+
</component>
15+
</module>

2023/day08_2/src/Main.kt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.io.File
2+
import java.util.*
3+
import kotlin.collections.ArrayList
4+
5+
fun main(args: Array<String>) {
6+
println("AOC 2023, Day 8, Part 2 starting!!!!")
7+
8+
var firstLine = false
9+
var directions = "None"
10+
val allNodes = ArrayList<Node>()
11+
File(args[0]).forEachLine {
12+
if(!firstLine) {
13+
directions = it
14+
firstLine = true
15+
} else if(it.isNotEmpty()) {
16+
val nodeName = it.substring(0..2)
17+
val connectionLeft = it.substring(7..9)
18+
val connectionRight = it.substring(12..14)
19+
allNodes.add(Node(nodeName, connectionLeft, connectionRight))
20+
}
21+
}
22+
23+
println("Total Nodes: ${allNodes.count()}")
24+
val nodesEndWithA = findNodesEndsWithA(allNodes)
25+
println("Finding nodes that end with A: $nodesEndWithA")
26+
val nodesEndWithZ = findNodesEndsWithZ(allNodes)
27+
println("Finding nodes that end with Z: $nodesEndWithZ")
28+
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")
37+
38+
var stepsStack = foundCount.pop()
39+
while(!foundCount.empty()) {
40+
stepsStack = lcm(stepsStack, foundCount.pop())
41+
}
42+
43+
println("Steps taken to traverse to all nodes end with Z: $stepsStack")
44+
45+
println("AOC 2023, Day 8, Part 2 completed!!!")
46+
}
47+
48+
fun lcm(a: Long, b: Long): Long {
49+
return (a / gcd(a, b)) * b
50+
}
51+
52+
fun gcd(a: Long, b: Long): Long {
53+
if (a == 0L) return b
54+
return gcd(b % a, a)
55+
}
56+
57+
58+
fun findNodesEndsWithA(nodes: ArrayList<Node>):ArrayList<Node> {
59+
val nodesEndsWithA = ArrayList<Node>()
60+
nodes.filter { it.nodeName.last() == 'A' }.toCollection(nodesEndsWithA)
61+
return nodesEndsWithA
62+
}
63+
64+
fun findNodesEndsWithZ(nodes: ArrayList<Node>):ArrayList<Node> {
65+
val nodesEndsWithZ = ArrayList<Node>()
66+
nodes.filter { it.nodeName.last() == 'Z' }.toCollection(nodesEndsWithZ)
67+
return nodesEndsWithZ
68+
}
69+
70+
fun traverseNodeIterative(directions: String, nodes: ArrayList<Node>, startNode: Node): Long {
71+
var totalSteps = 0L
72+
var nextStep = 0
73+
var currentNode = Node(startNode.nodeName, startNode.connectionLeft, startNode.connectionRight)
74+
var nextNode:Node
75+
while(currentNode.nodeName.last() != 'Z') {
76+
val leftOrRight = directions[nextStep]
77+
nextNode = if(leftOrRight.compareTo('L') == 0) {
78+
nodes.find { it.nodeName == currentNode.connectionLeft }!!
79+
} else {
80+
nodes.find { it.nodeName == currentNode.connectionRight }!!
81+
}
82+
currentNode = nextNode
83+
84+
nextStep++
85+
totalSteps++
86+
if(nextStep >= directions.length) {
87+
nextStep = 0
88+
}
89+
}
90+
91+
return totalSteps
92+
}
93+
94+
data class Node (val nodeName:String, val connectionLeft:String, val connectionRight:String)

0 commit comments

Comments
 (0)