@@ -3,67 +3,58 @@ package adventofcode2023
3
3
import java.io.File
4
4
5
5
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
13
13
var steps = 0
14
- var i = 0
15
- var location = " AAA"
16
14
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
20
17
steps++
21
18
}
22
19
23
- println ( steps)
20
+ return steps.toLong( )
24
21
}
25
22
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
32
27
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
+ }
37
33
38
- while (mb != 0L ) {
39
- remainder = ma % mb
40
- ma = mb
41
- mb = remainder
42
- }
34
+ return a * b / ma
35
+ }
43
36
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
+ }
46
40
41
+ fun part2 (instructions : String , nodes : Map <String , Pair <String , String >>) {
47
42
println (
48
43
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" )
60
45
}.reduce { acc, i -> lcm(acc, i) }
61
46
)
62
47
}
63
48
}
64
49
65
50
fun main () {
66
51
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)
69
60
}
0 commit comments