Day02 2021
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user