-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,047 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/adventofcode/year2024/Day02RedNosedReports.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.