Skip to content

Commit

Permalink
[2024/7] Bridge Repair (Part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 7, 2024
1 parent a52ef94 commit 2058e07
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| 2021 |||||||||||| ||| | | | | | | | | | | | 26 |
| 2022 |||||||||||||| | | | | | | || | | || 28 |
| 2023 |||||||| | | | | | | | | | | | | | | | | | | 12 |
| 2024 ||||||| | | | | | | | | | | | | | | | | | | | 13 |
| 2024 ||||||| | | | | | | | | | | | | | | | | | | | 14 |

## 🛷 How to run

Expand Down Expand Up @@ -170,7 +170,7 @@ e.g. `HandyHaversacks`)*
| | 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [[Code](src/main/kotlin/adventofcode/year2024/Day04CeresSearch.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day04CeresSearchSpec.kt)] | `2483` | `1925` |
| | 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [[Code](src/main/kotlin/adventofcode/year2024/Day05PrintQueue.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day05PrintQueueSpec.kt)] | `6498` | `5017` |
| | 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [[Code](src/main/kotlin/adventofcode/year2024/Day06GuardGallivant.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day06GuardGallivantSpec.kt)] | `5212` | `1767` |
| | 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [[Code](src/main/kotlin/adventofcode/year2024/Day07BridgeRepair.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day07BridgeRepairSpec.kt)] | `8401132154762` | |
| | 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [[Code](src/main/kotlin/adventofcode/year2024/Day07BridgeRepair.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day07BridgeRepairSpec.kt)] | `8401132154762` | `95297119227552` |

## 🕯️ Useful commands

Expand Down
24 changes: 17 additions & 7 deletions src/main/kotlin/adventofcode/year2024/Day07BridgeRepair.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ import adventofcode.Puzzle
import adventofcode.PuzzleInput

class Day07BridgeRepair(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override fun partOne() =
private val equations by lazy {
input
.lines()
.mapNotNull { equation ->
val result = equation.split(": ").first().toLong()
val numbers = equation.split(": ").last().split(" ").map(String::toLong)
.map { equation ->
val (result, numbers) = equation.split(": ")
result.toLong() to numbers.split(" ").map(String::toLong)
}
}

override fun partOne() = equations.calibrationResult(setOf({ a, b -> a + b }, { a, b -> a * b }))

override fun partTwo() = equations.calibrationResult(setOf({ a, b -> a + b }, { a, b -> a * b }, { a, b -> "$a$b".toLong() }))

companion object {
private fun List<Pair<Long, List<Long>>>.calibrationResult(operators: Set<(Long, Long) -> Long>) =
filter { (result, numbers) ->
val results =
numbers
.drop(1)
.fold(setOf(numbers.first())) { candidates, number ->
candidates.flatMap { candidate -> setOf(candidate + number, candidate * number) }.toSet()
candidates.flatMap { candidate -> operators.map { operator -> operator(candidate, number) } }.toSet()
}

if (result in results) result else null
result in results
}
.sum()
.sumOf { (result, _) -> result }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package adventofcode.year2024

import adventofcode.PuzzleBaseSpec

class Day07BridgeRepairSpec : PuzzleBaseSpec(3749)
class Day07BridgeRepairSpec : PuzzleBaseSpec(3749, 11387)

0 comments on commit 2058e07

Please sign in to comment.