diff --git a/src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt b/src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt index 9603c8f..cc49818 100644 --- a/src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt +++ b/src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt @@ -5,20 +5,26 @@ import adventofcode.PuzzleInput import kotlin.math.abs class Day01HistorianHysteria(customInput: PuzzleInput? = null) : Puzzle(customInput) { - private val lists by lazy { - input.lines().map { line -> line.split(" ").mapNotNull(String::toIntOrNull) } - } + private fun parseInput(): Pair, List> { + val lists = input.lines().map { line -> line.split(" ").mapNotNull(String::toIntOrNull) } + val leftList = lists.map { it.first() } + val rightList = lists.map { it.last() } - private val leftList by lazy { lists.map { it.first() } } - private val rightList by lazy { lists.map { it.last() } } + return leftList to rightList + } override fun partOne() = - leftList - .sorted() - .zip(rightList.sorted()) - .sumOf { (left, right) -> abs(left - right) } + parseInput() + .let { (leftList, rightList) -> + leftList + .sorted() + .zip(rightList.sorted()) + .sumOf { (left, right) -> abs(left - right) } + } override fun partTwo() = - leftList - .sumOf { left -> left * rightList.count { right -> left == right } } + parseInput() + .let { (leftList, rightList) -> + leftList.sumOf { left -> left * rightList.count { right -> left == right } } + } } diff --git a/src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt b/src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt index d165bef..7297f80 100644 --- a/src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt +++ b/src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt @@ -2,36 +2,25 @@ package adventofcode.year2024 import adventofcode.Puzzle import adventofcode.PuzzleInput -import kotlin.math.abs class Day02RedNosedReports(customInput: PuzzleInput? = null) : Puzzle(customInput) { override val name = "Red-Nosed Reports" - private val reports by lazy { - input.lines().map { report -> report.split(" ").mapNotNull(String::toIntOrNull) } - } + private fun parseInput() = input.lines().map { report -> report.split(" ").mapNotNull(String::toIntOrNull) } - override fun partOne() = reports.count { report -> report.isSafeReport() } + override fun partOne() = parseInput().count { report -> report.isSafeReport() } - override fun partTwo() = reports.count { report -> report.isAlmostSafeReport() } + override fun partTwo() = parseInput().count { report -> report.isSafeReportDampened() } companion object { - private fun List.isSafeReport() = (isStrictlyIncreasing() || isStrictlyDecreasing()) && differsByAtLeast1AndAtMost3() - - private fun List.isAlmostSafeReport(): Boolean { - for (i in -1 until size) { - if (filterIndexed { j, _ -> i != j }.isSafeReport()) { - return true - } - } - - return false + private fun List.isSafeReport(): Boolean { + val deltas = zipWithNext().map { (a, b) -> b - a } + return deltas.all { delta -> delta in -3..-1 } || deltas.all { delta -> delta in 1..3 } } - private fun List.isStrictlyIncreasing() = zipWithNext().all { (a, b) -> a < b } - - private fun List.isStrictlyDecreasing() = zipWithNext().all { (a, b) -> a > b } - - private fun List.differsByAtLeast1AndAtMost3() = zipWithNext().all { (a, b) -> abs(a - b) in 1..3 } + private fun List.isSafeReportDampened() = + indices.any { i -> + filterIndexed { j, _ -> i != j }.isSafeReport() + } } }