mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 00:56:10 +00:00
day 19, my fastest times yet!
This commit is contained in:
parent
76aecbc04a
commit
653e4a7b3e
4 changed files with 109 additions and 15 deletions
9
src/day19/README.md
Normal file
9
src/day19/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 19 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/19)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
85
src/day19/index.ts
Normal file
85
src/day19/index.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => {
|
||||
const [a,b] = rawInput.split('\n\n')
|
||||
const towels = a.split(', ')
|
||||
const patterns = b.split('\n')
|
||||
return {
|
||||
towels,
|
||||
patterns,
|
||||
}
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const {towels, patterns} = parseInput(rawInput)
|
||||
const reg = new RegExp(`^(${towels.join('|')})+$`)
|
||||
return patterns.reduce((total, pattern) => {
|
||||
return total + (reg.test(pattern) ? 1 : 0)
|
||||
}, 0)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const {towels, patterns} = parseInput(rawInput)
|
||||
return patterns.reduce((total, pattern) => {
|
||||
const cache = new Map<number, number>()
|
||||
const ways = (index: number) => {
|
||||
if (cache.has(index)) {
|
||||
return cache.get(index)
|
||||
}
|
||||
if (index === pattern.length) {
|
||||
cache.set(index, 1)
|
||||
return 1
|
||||
}
|
||||
const ans = towels.reduce((total2, t) => {
|
||||
if (pattern.slice(index,index+t.length) === t) {
|
||||
return total2 + ways(index + t.length)
|
||||
}
|
||||
return total2
|
||||
}, 0)
|
||||
cache.set(index, ans)
|
||||
return ans
|
||||
}
|
||||
return total + ways(0)
|
||||
}, 0)
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `r, wr, b, g, bwu, rb, gb, br
|
||||
|
||||
brwrr
|
||||
bggr
|
||||
gbbr
|
||||
rrbgbr
|
||||
ubwu
|
||||
bwurrg
|
||||
brgr
|
||||
bbrgwb`,
|
||||
expected: 6,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `r, wr, b, g, bwu, rb, gb, br
|
||||
|
||||
brwrr
|
||||
bggr
|
||||
gbbr
|
||||
rrbgbr
|
||||
ubwu
|
||||
bwurrg
|
||||
brgr
|
||||
bbrgwb`,
|
||||
expected: 16,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue