-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.kt
36 lines (34 loc) · 997 Bytes
/
Day2.kt
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
package com.github.ephemient.aoc2021
/** [Day 2](https://adventofcode.com/2021/day/2): Dive! */
class Day2(private val lines: List<String>) {
fun part1(): Int {
var x = 0
var y = 0
for (line in lines) {
val (prefix, suffix) = line.split(" ", limit = 2)
when (prefix) {
"forward" -> x += suffix.toInt()
"down" -> y += suffix.toInt()
"up" -> y -= suffix.toInt()
}
}
return x * y
}
fun part2(): Int {
var x = 0
var y = 0
var depth = 0
for (line in lines) {
val (prefix, suffix) = line.split(" ", limit = 2)
when (prefix) {
"forward" -> suffix.toInt().let {
x += it
y += it * depth
}
"down" -> depth += suffix.toInt()
"up" -> depth -= suffix.toInt()
}
}
return x * y
}
}