mirror of
https://github.com/chylex/Advent-of-Code.git
synced 2025-07-29 15:59:07 +02:00
Add 2021 - Day 2 - Part 2
This commit is contained in:
parent
457d8ef8a8
commit
8004a505e4
@ -4,6 +4,16 @@ fun main() {
|
|||||||
val lines = File("input/1.txt").readLines()
|
val lines = File("input/1.txt").readLines()
|
||||||
val directions = lines.map { line -> line.split(' ', limit = 2).let { it[0] to it[1].toInt() } }
|
val directions = lines.map { line -> line.split(' ', limit = 2).let { it[0] to it[1].toInt() } }
|
||||||
|
|
||||||
|
println("Part 1:")
|
||||||
|
part1(directions)
|
||||||
|
|
||||||
|
println()
|
||||||
|
|
||||||
|
println("Part 2:")
|
||||||
|
part2(directions)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun part1(directions: List<Pair<String, Int>>) {
|
||||||
var position = 0
|
var position = 0
|
||||||
var depth = 0
|
var depth = 0
|
||||||
|
|
||||||
@ -19,3 +29,24 @@ fun main() {
|
|||||||
println("Depth: $depth")
|
println("Depth: $depth")
|
||||||
println("Multiplied: ${position * depth}")
|
println("Multiplied: ${position * depth}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun part2(directions: List<Pair<String, Int>>) {
|
||||||
|
var position = 0
|
||||||
|
var depth = 0
|
||||||
|
var aim = 0
|
||||||
|
|
||||||
|
for ((direction, distance) in directions) {
|
||||||
|
when (direction) {
|
||||||
|
"forward" -> {
|
||||||
|
position += distance
|
||||||
|
depth += aim * distance
|
||||||
|
}
|
||||||
|
"up" -> aim -= distance
|
||||||
|
"down" -> aim += distance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Position: $position")
|
||||||
|
println("Depth: $depth")
|
||||||
|
println("Multiplied: ${position * depth}")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user