Day02 2021
This commit is contained in:
parent
15b11a191d
commit
b9a8e7585b
40
2021/src/main/kotlin/Day02.kt
Normal file
40
2021/src/main/kotlin/Day02.kt
Normal file
@ -0,0 +1,40 @@
|
||||
package be.vandewalleh.aoc.days
|
||||
|
||||
import be.vandewalleh.aoc.utils.input.Day
|
||||
import be.vandewalleh.aoc.utils.input.Text
|
||||
|
||||
@Day
|
||||
class Day02(@Text input: String) {
|
||||
private val lines = input.lines().map { it.split(' ').let { it[0] to it[1].toInt() } }
|
||||
|
||||
fun part1(): Any {
|
||||
var horizontalPosition = 0
|
||||
var depth = 0
|
||||
lines.forEach { (direction, value) ->
|
||||
when (direction) {
|
||||
"forward" -> horizontalPosition += value
|
||||
"down" -> depth += value
|
||||
"up" -> depth -= value
|
||||
}
|
||||
}
|
||||
return horizontalPosition * depth
|
||||
}
|
||||
|
||||
fun part2(): Any {
|
||||
var horizontalPosition = 0
|
||||
var depth = 0
|
||||
var aim = 0
|
||||
lines.forEach { (direction, value) ->
|
||||
when (direction) {
|
||||
"forward" -> {
|
||||
horizontalPosition += value
|
||||
depth += aim * value
|
||||
}
|
||||
"down" -> aim += value
|
||||
"up" -> aim -= value
|
||||
}
|
||||
}
|
||||
return horizontalPosition * depth
|
||||
}
|
||||
|
||||
}
|
||||
1000
2021/src/main/resources/day02.txt
Normal file
1000
2021/src/main/resources/day02.txt
Normal file
File diff suppressed because it is too large
Load Diff
18
2021/src/test/kotlin/Day02Test.kt
Normal file
18
2021/src/test/kotlin/Day02Test.kt
Normal file
@ -0,0 +1,18 @@
|
||||
package be.vandewalleh.aoc.days
|
||||
|
||||
class Day02Test : BaseDayTest(2) {
|
||||
override val example = """
|
||||
forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2
|
||||
""".trimIndent()
|
||||
|
||||
override val part1Example = 150
|
||||
override val part2Example = 900
|
||||
|
||||
override val part1Answer = 1938402
|
||||
override val part2Answer = 1947878632
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user