Skip to content

Commit

Permalink
[2024/2] Red-Nosed Reports (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 2, 2024
1 parent c8de6a4 commit c530f6c
Show file tree
Hide file tree
Showing 5 changed files with 1,047 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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-2%2F50-0f0f23" alt="2024 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>

## 🛷 How to run

Expand Down Expand Up @@ -161,6 +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` | |

## 🕯️ Useful commands

Expand Down
34 changes: 34 additions & 0 deletions src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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"

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

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
}

if (change.any { abs(it) > 3 }) {
return false
}

return true
}
}
}
Loading

0 comments on commit c530f6c

Please sign in to comment.