1
0
Fork 0

day(02): refactor

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-04 15:54:39 +01:00
parent e3e721b6b4
commit 91aa2e41b7

View file

@ -6,53 +6,50 @@ enum class Command {
FORWARD, DOWN, UP FORWARD, DOWN, UP
} }
data class Position(val horizontal: Int = 0, val depth: Int = 0, val aim: Int = 0)
data class Instruction(val type: Command, val value: Int)
fun parseInstruction(instruction: String): Instruction {
val splitLine = instruction.split(' ')
check(splitLine.size == 2)
val value = splitLine[1].toInt()
when (splitLine[0]) {
"forward" -> return Instruction(Command.FORWARD, value)
"down" -> return Instruction(Command.DOWN, value)
"up" -> return Instruction(Command.UP, value)
}
throw IllegalArgumentException("Cannot parse instruction")
}
fun toCommands(input: List<String>): List<Instruction> = input.map { parseInstruction(it) }
fun part1(input: List<Instruction>): Int {
val finalPosition = input.fold(Position()) { pos, instruction ->
when (instruction.type) {
Command.FORWARD -> pos.copy(horizontal = pos.horizontal + instruction.value)
Command.UP -> pos.copy(depth = pos.depth - instruction.value)
Command.DOWN -> pos.copy(depth = pos.depth + instruction.value)
}
}
return finalPosition.horizontal * finalPosition.depth
}
fun part2(input: List<Instruction>): Int {
val finalPosition = input.fold(Position()) { pos, instruction ->
when (instruction.type) {
Command.FORWARD -> pos.copy(
horizontal = pos.horizontal + instruction.value,
depth = pos.depth + instruction.value * pos.aim
)
Command.UP -> pos.copy(aim = pos.aim - instruction.value)
Command.DOWN -> pos.copy(aim = pos.aim + instruction.value)
}
}
return finalPosition.horizontal * finalPosition.depth
}
fun main() { fun main() {
data class Position(val horizontal: Int = 0, val depth: Int = 0, val aim: Int = 0)
data class Instruction(val type: Command, val value: Int)
fun parseInstruction(instruction: String): Instruction {
val splitLine = instruction.split(' ')
check(splitLine.size == 2)
val value = splitLine[1].toInt()
when (splitLine[0]) {
"forward" -> return Instruction(Command.FORWARD, value)
"down" -> return Instruction(Command.DOWN, value)
"up" -> return Instruction(Command.UP, value)
}
throw IllegalArgumentException("Cannot parse instruction")
}
fun toCommands(input: List<String>): List<Instruction> {
return input.map { parseInstruction(it) }
}
fun part1(input: List<Instruction>): Int {
val finalPosition = input.fold(Position()) { pos, instruction ->
when (instruction.type) {
Command.FORWARD -> pos.copy(horizontal = pos.horizontal + instruction.value)
Command.UP -> pos.copy(depth = pos.depth - instruction.value)
Command.DOWN -> pos.copy(depth = pos.depth + instruction.value)
}
}
return finalPosition.horizontal * finalPosition.depth
}
fun part2(input: List<Instruction>): Int {
val finalPosition = input.fold(Position()) { pos, instruction ->
when (instruction.type) {
Command.FORWARD -> pos.copy(
horizontal = pos.horizontal + instruction.value,
depth = pos.depth + instruction.value * pos.aim
)
Command.UP -> pos.copy(aim = pos.aim - instruction.value)
Command.DOWN -> pos.copy(aim = pos.aim + instruction.value)
}
}
return finalPosition.horizontal * finalPosition.depth
}
val testInput = toCommands(readInput(2, "test_input")) val testInput = toCommands(readInput(2, "test_input"))
val input = toCommands(readInput(2, "input")) val input = toCommands(readInput(2, "input"))