-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2.kts
42 lines (37 loc) · 1.05 KB
/
day_2.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.File
fun day2Sol1() {
var horiz = 0
var depth = 0
File("in/input2.txt")
.readLines()
.mapNotNull { "^(\\w+) (\\d+)$".toRegex().find(it)?.destructured }
.forEach { (dir, amount) ->
when (dir) {
"forward" -> horiz += amount.toInt()
"down" -> depth += amount.toInt()
"up" -> depth -= amount.toInt()
}
}
.also { println("sol1: ${horiz * depth}") }
}
fun day2Sol2() {
var horiz = 0
var depth = 0
var aim = 0
File("in/input2.txt")
.readLines()
.mapNotNull { "^(\\w+) (\\d+)$".toRegex().find(it)?.destructured }
.forEach { (dir, amount) ->
when (dir) {
"down" -> aim += amount.toInt()
"up" -> aim -= amount.toInt()
"forward" -> {
horiz += amount.toInt()
depth += aim * amount.toInt()
}
}
}
.also { println("sol2: ${horiz * depth}") }
}
day2Sol1()
day2Sol2()