diff --git a/days/src/main/kotlin/Day05.kt b/days/src/main/kotlin/Day05.kt index 8782a0c..6f24c05 100644 --- a/days/src/main/kotlin/Day05.kt +++ b/days/src/main/kotlin/Day05.kt @@ -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>) { - 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) = 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()) {