Very minor day 2 refinement.

This commit is contained in:
Chris Hodges 2024-12-04 10:03:28 +01:00
parent 4bf79c0e89
commit 782d12c048

View File

@ -50,21 +50,18 @@ fun main() {
fun checkGood(i: List<Int>): Boolean { fun checkGood(i: List<Int>): Boolean {
val dir = i[1] - i[0] val dir = i[1] - i[0]
return if (dir < 0) { return if (dir < 0) {
!i.runningReduce { acc, elem -> if ((elem < acc) and (elem >= acc - 3)) elem else Int.MIN_VALUE }.contains(Int.MIN_VALUE) !i.runningReduce { acc, elem -> if ((elem < acc) and (elem >= acc - 3)) elem else Int.MIN_VALUE }
.contains(Int.MIN_VALUE)
} else if (dir > 0) { } else if (dir > 0) {
!i.runningReduce { acc, elem -> if ((elem > acc) && (elem <= acc + 3)) elem else Int.MAX_VALUE }.contains(Int.MAX_VALUE) !i.runningReduce { acc, elem -> if ((elem > acc) && (elem <= acc + 3)) elem else Int.MAX_VALUE }
.contains(Int.MAX_VALUE)
} else { } else {
false false
} }
} }
fun part1(input: List<String>): Int { fun part1(input: List<String>): Int {
val ilist = input.map { it.splitInts() } return input.map { it.splitInts() }.count(::checkGood)
var sum = 0
for (i in ilist) {
if (checkGood(i)) sum++
}
return sum
} }
fun part2(input: List<String>): Int { fun part2(input: List<String>): Int {