1
0
This commit is contained in:
Hubert Van De Walle 2020-12-05 08:07:41 +01:00
parent e65fbe9f63
commit 23e3b517e9

View File

@ -4,44 +4,24 @@ 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
private val ids = input.value.map {
it.replace("F", "0")
.replace("B", "1")
.replace("L", "0")
.replace("R", "1")
.toInt(2)
}
fun part1() = ids.maxOrNull()
fun part2() = ids.sorted()
.windowed(size = 2, step = 1)
.find { (a, b) -> b - a > 1 }!!
.first() + 1
}
fun main() = with(createDay<Day05>()) {