Skip to content

Commit

Permalink
[2024/2] Red-Nosed Reports (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 2, 2024
1 parent c530f6c commit 0862e77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<a href="https://adventofcode.com/2021"><img src="https://img.shields.io/badge/%E2%AD%90%202021-26%2F50-0f0f23" alt="2021 Progress" /></a>
<a href="https://adventofcode.com/2022"><img src="https://img.shields.io/badge/%E2%AD%90%202022-28%2F50-0f0f23" alt="2022 Progress" /></a>
<a href="https://adventofcode.com/2023"><img src="https://img.shields.io/badge/%E2%AD%90%202023-12%2F50-0f0f23" alt="2023 Progress" /></a>
<a href="https://adventofcode.com/2024"><img src="https://img.shields.io/badge/%E2%AD%90%202024-3%2F50-0f0f23" alt="2024 Progress" /></a>
<a href="https://adventofcode.com/2024"><img src="https://img.shields.io/badge/%E2%AD%90%202024-4%2F50-0f0f23" alt="2024 Progress" /></a>

## 🛷 How to run

Expand Down Expand Up @@ -161,7 +161,7 @@ e.g. `HandyHaversacks`)*
| | 6 | [Wait For It](https://adventofcode.com/2023/day/6) | [[Code](src/main/kotlin/adventofcode/year2023/Day06WaitForIt.kt)]&nbsp;[[Test](src/test/kotlin/adventofcode/year2023/Day06WaitForItSpec.kt)] | `2269432` | `35865985` |
| | 7 | [Camel Cards](https://adventofcode.com/2023/day/7) | [[Code](src/main/kotlin/adventofcode/year2023/Day07CamelCards.kt)]&nbsp;[[Test](src/test/kotlin/adventofcode/year2023/Day07CamelCardsSpec.kt)] | `250474325` | |
| [**2024**](https://adventofcode.com/2024) | 1 | [Historian Hysteria](https://adventofcode.com/2024/day/1) | [[Code](src/main/kotlin/adventofcode/year2024/Day01HistorianHysteria.kt)]&nbsp;[[Test](src/test/kotlin/adventofcode/year2024/Day01HistorianHysteriaSpec.kt)] | `1970720` | `17191599` |
| | 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [[Code](src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt)]&nbsp;[[Test](src/test/kotlin/adventofcode/year2024/Day02RedNosedReportsSpec.kt)] | `572` | |
| | 2 | [Red-Nosed Reports](https://adventofcode.com/2024/day/2) | [[Code](src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt)]&nbsp;[[Test](src/test/kotlin/adventofcode/year2024/Day02RedNosedReportsSpec.kt)] | `572` | `612` |

## 🕯️ Useful commands

Expand Down
37 changes: 20 additions & 17 deletions src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@ import kotlin.math.abs
class Day02RedNosedReports(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override val name = "Red-Nosed Reports"

override fun partOne() =
input
.lines()
.map { report -> report.split(" ").mapNotNull(String::toIntOrNull) }
.count { report -> report.isSafeReport() }
private val reports by lazy {
input.lines().map { report -> report.split(" ").mapNotNull(String::toIntOrNull) }
}

override fun partOne() = reports.count { report -> report.isSafeReport() }

override fun partTwo() = reports.count { report -> report.isAlmostSafeReport() }

companion object {
private fun List<Int>.isSafeReport(): Boolean {
val change =
this
.zipWithNext()
.map { (a, b) -> b - a }

if (!change.all { it > 0 } && !change.all { it < 0 }) {
return false
}
private fun List<Int>.isSafeReport() = (isStrictlyIncreasing() || isStrictlyDecreasing()) && differsByAtLeast1AndAtMost3()

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

return true
return false
}

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 }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package adventofcode.year2024

import adventofcode.PuzzleBaseSpec

class Day02RedNosedReportsSpec : PuzzleBaseSpec(2)
class Day02RedNosedReportsSpec : PuzzleBaseSpec(2, 4)

0 comments on commit 0862e77

Please sign in to comment.