Skip to content

Commit

Permalink
Refactor 2024/1 and 2024/2
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 3, 2024
1 parent 4134ef2 commit 6998398
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
28 changes: 17 additions & 11 deletions src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>, List<Int>> {
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 } }
}
}
31 changes: 10 additions & 21 deletions src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>.isSafeReport() = (isStrictlyIncreasing() || isStrictlyDecreasing()) && differsByAtLeast1AndAtMost3()

private fun List<Int>.isAlmostSafeReport(): Boolean {
for (i in -1 until size) {
if (filterIndexed { j, _ -> i != j }.isSafeReport()) {
return true
}
}

return false
private fun List<Int>.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<Int>.isStrictlyIncreasing() = zipWithNext().all { (a, b) -> a < b }

private fun List<Int>.isStrictlyDecreasing() = zipWithNext().all { (a, b) -> a > b }

private fun List<Int>.differsByAtLeast1AndAtMost3() = zipWithNext().all { (a, b) -> abs(a - b) in 1..3 }
private fun List<Int>.isSafeReportDampened() =
indices.any { i ->
filterIndexed { j, _ -> i != j }.isSafeReport()
}
}
}

0 comments on commit 6998398

Please sign in to comment.