Skip to content

Commit

Permalink
[2024/19] Linen Layout (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfolta committed Dec 20, 2024
1 parent bf36550 commit c6b4a14
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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 | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | | β˜† | β˜† | | | | | | | | | 30 |
| 2024 | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | β˜… | | β˜† | β˜† | | β˜† | | | | | | | 31 |

## πŸ›· How to run

Expand Down Expand Up @@ -180,6 +180,7 @@ e.g. `HandyHaversacks`)*
| | 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [[Code](src/main/kotlin/adventofcode/year2024/Day14RestroomRedoubt.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day14RestroomRedoubtSpec.kt)] | `224357412` | `7083` |
| | 16 | [Reindeer Maze](https://adventofcode.com/2024/day/16) | [[Code](src/main/kotlin/adventofcode/year2024/Day16ReindeerMaze.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day16ReindeerMazeSpec.kt)] | `98484` | |
| | 17 | [Chronospatial Computer](https://adventofcode.com/2024/day/17) | [[Code](src/main/kotlin/adventofcode/year2024/Day17ChronospatialComputer.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day17ChronospatialComputerSpec.kt)] | `5,1,3,4,3,7,2,1,7` | |
| | 19 | [Linen Layout](https://adventofcode.com/2024/day/19) | [[Code](src/main/kotlin/adventofcode/year2024/Day19LinenLayout.kt)] [[Test](src/test/kotlin/adventofcode/year2024/Day19LinenLayoutSpec.kt)] | `267` | |

## πŸ•―οΈ Useful commands

Expand Down
28 changes: 28 additions & 0 deletions src/main/kotlin/adventofcode/year2024/Day19LinenLayout.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package adventofcode.year2024

import adventofcode.Puzzle
import adventofcode.PuzzleInput

class Day19LinenLayout(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val patterns by lazy { input.lines().first().split(", ") }

private val designs by lazy { input.lines().drop(2) }

override fun partOne() = designs.count { design -> design.patternCombinations(patterns) > 0 }

companion object {
private fun String.patternCombinations(
patterns: List<String>,
cache: MutableMap<String, Long> = mutableMapOf(),
): Long =
when {
isEmpty() -> 1
else ->
cache.getOrPut(this) {
patterns.filter { pattern -> startsWith(pattern) }.sumOf { pattern ->
removePrefix(pattern).patternCombinations(patterns, cache)
}
}
}
}
}
Loading

0 comments on commit c6b4a14

Please sign in to comment.