-
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
890 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
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/adventofcode/year2024/Day07BridgeRepair.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,24 @@ | ||
package adventofcode.year2024 | ||
|
||
import adventofcode.Puzzle | ||
import adventofcode.PuzzleInput | ||
|
||
class Day07BridgeRepair(customInput: PuzzleInput? = null) : Puzzle(customInput) { | ||
override fun partOne() = | ||
input | ||
.lines() | ||
.mapNotNull { equation -> | ||
val result = equation.split(": ").first().toLong() | ||
val numbers = equation.split(": ").last().split(" ").map(String::toLong) | ||
|
||
val results = | ||
numbers | ||
.drop(1) | ||
.fold(setOf(numbers.first())) { candidates, number -> | ||
candidates.flatMap { candidate -> setOf(candidate + number, candidate * number) }.toSet() | ||
} | ||
|
||
if (result in results) result else null | ||
} | ||
.sum() | ||
} |
Oops, something went wrong.