Mmh
This commit is contained in:
parent
e65fbe9f63
commit
23e3b517e9
@ -4,44 +4,24 @@ import be.vandewalleh.aoc.utils.input.Day
|
|||||||
import be.vandewalleh.aoc.utils.input.Input
|
import be.vandewalleh.aoc.utils.input.Input
|
||||||
import be.vandewalleh.aoc.utils.input.Lines
|
import be.vandewalleh.aoc.utils.input.Lines
|
||||||
import be.vandewalleh.aoc.utils.input.createDay
|
import be.vandewalleh.aoc.utils.input.createDay
|
||||||
import kotlin.math.abs
|
|
||||||
|
|
||||||
@Day(5)
|
@Day(5)
|
||||||
class Day05(@Lines val input: Input<List<String>>) {
|
class Day05(@Lines val input: Input<List<String>>) {
|
||||||
|
|
||||||
private fun row(line: String) = line.take(7)
|
private val ids = input.value.map {
|
||||||
.replace("F", "0")
|
it.replace("F", "0")
|
||||||
.replace("B", "1")
|
.replace("B", "1")
|
||||||
.let { Integer.parseInt(it, 2) }
|
.replace("L", "0")
|
||||||
|
.replace("R", "1")
|
||||||
private fun col(line: String) = line.takeLast(3)
|
.toInt(2)
|
||||||
.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 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>()) {
|
fun main() = with(createDay<Day05>()) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user