1
0
This commit is contained in:
2020-12-05 07:10:12 +01:00
parent 4008858df8
commit e65fbe9f63
3 changed files with 878 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Input
import be.vandewalleh.aoc.utils.input.Lines
import be.vandewalleh.aoc.utils.input.createDay
import kotlin.math.abs
@Day(5)
class Day05(@Lines val input: Input<List<String>>) {
private fun row(line: String) = line.take(7)
.replace("F", "0")
.replace("B", "1")
.let { Integer.parseInt(it, 2) }
private fun col(line: String) = line.takeLast(3)
.replace("L", "0")
.replace("R", "1")
.let { Integer.parseInt(it, 2) }
private fun id(seat: Pair<Int, Int>) = seat.first.times(8) + seat.second
fun part1() = input.value
.map { row(it) to col(it) }
.map { id(it) }
.maxOrNull()!!
fun part2(): Int {
val seats = input.value.map { row(it) to col(it) }
val front = seats.minByOrNull { it.first }!!.first
val back = seats.maxByOrNull { it.first }!!.first
return seats
.asSequence()
.filterNot { it.first == front }
.filterNot { it.first == back }
.map { id(it) }
.sorted()
.windowed(size = 2, step = 1)
.find { (a, b) -> abs(a - b) > 1 }!!
.first() + 1
}
}
fun main() = with(createDay<Day05>()) {
println(part1())
println(part2())
}