Skip to content

Commit c0981c4

Browse files
committed
Reformat 2023 Day 08 solutions
1 parent c795dbe commit c0981c4

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

adventofcode2023/Day08.kt

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,58 @@ package adventofcode2023
33
import java.io.File
44

55
object Day08 {
6-
fun part1(inputs: List<String>) {
7-
val instructions = inputs[0]
8-
val nodes = inputs.drop(2).associate {
9-
val node = it.replace(Regex("[^A-Z ]"), "").replace(Regex("\\s+"), " ").split(" ")
10-
node[0] to Pair(node[1], node[2])
11-
}
12-
6+
private fun getSteps(
7+
instructions: String,
8+
nodes: Map<String, Pair<String, String>>,
9+
location: String,
10+
endsWith: String
11+
): Long {
12+
var current = location
1313
var steps = 0
14-
var i = 0
15-
var location = "AAA"
1614

17-
while (location != "ZZZ") {
18-
location = if (instructions[i] == 'L') nodes[location]!!.first else nodes[location]!!.second
19-
if (i + 1 == instructions.length) i = 0 else i++
15+
while (!current.endsWith(endsWith)) {
16+
current = if (instructions[steps % instructions.length] == 'L') nodes[current]!!.first else nodes[current]!!.second
2017
steps++
2118
}
2219

23-
println(steps)
20+
return steps.toLong()
2421
}
2522

26-
fun part2(inputs: List<String>) {
27-
val instructions = inputs[0]
28-
val nodes = inputs.drop(2).associate {
29-
val node = it.replace(Regex("[^A-Z ]"), "").replace(Regex("\\s+"), " ").split(" ")
30-
node[0] to Pair(node[1], node[2])
31-
}
23+
private fun lcm(a: Long, b: Long): Long {
24+
var ma = a
25+
var mb = b
26+
var remainder: Long
3227

33-
fun lcm(a: Long, b: Long): Long {
34-
var ma = a
35-
var mb = b
36-
var remainder: Long
28+
while (mb != 0L) {
29+
remainder = ma % mb
30+
ma = mb
31+
mb = remainder
32+
}
3733

38-
while (mb != 0L) {
39-
remainder = ma % mb
40-
ma = mb
41-
mb = remainder
42-
}
34+
return a * b / ma
35+
}
4336

44-
return a * b / ma
45-
}
37+
fun part1(instructions: String, nodes: Map<String, Pair<String, String>>) {
38+
println(getSteps(instructions, nodes, "AAA", "ZZZ"))
39+
}
4640

41+
fun part2(instructions: String, nodes: Map<String, Pair<String, String>>) {
4742
println(
4843
nodes.filter { it.key.endsWith('A') }.map {
49-
var steps = 0
50-
var i = 0
51-
var location = it.key
52-
53-
while (!location.endsWith('Z')) {
54-
location = if (instructions[i] == 'L') nodes[location]!!.first else nodes[location]!!.second
55-
if (i + 1 == instructions.length) i = 0 else i++
56-
steps++
57-
}
58-
59-
steps.toLong()
44+
getSteps(instructions, nodes, it.key, "Z")
6045
}.reduce { acc, i -> lcm(acc, i) }
6146
)
6247
}
6348
}
6449

6550
fun main() {
6651
val inputs = File("resources/adventofcode2023/Day08.txt").readLines()
67-
Day08.part1(inputs)
68-
Day08.part2(inputs)
52+
val instructions = inputs[0]
53+
val nodes = inputs.drop(2).associate {
54+
val node = it.replace(Regex("[^A-Z ]"), "").replace(Regex("\\s+"), " ").split(" ")
55+
node[0] to Pair(node[1], node[2])
56+
}
57+
58+
Day08.part1(instructions, nodes)
59+
Day08.part2(instructions, nodes)
6960
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy