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
|
@ -286,16 +286,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "371",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 1.906905
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "650354687260341",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 171.177121
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
18
README.md
18
README.md
|
@ -29,7 +29,7 @@
|
||||||
[](src/day16)
|
[](src/day16)
|
||||||
[](src/day17)
|
[](src/day17)
|
||||||
[](src/day18)
|
[](src/day18)
|
||||||

|
[](src/day19)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -188,16 +188,16 @@ Both parts: 8772.994ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 18
|
Day 18
|
||||||
Time part 1: 26.466ms
|
Time part 1: 27.726ms
|
||||||
Time part 2: 1115.347ms
|
Time part 2: 15841.686ms
|
||||||
Both parts: 1141.812ms
|
Both parts: 15869.412ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 19
|
Day 19
|
||||||
Time part 1: -
|
Time part 1: 1.907ms
|
||||||
Time part 2: -
|
Time part 2: 171.177ms
|
||||||
Both parts: -
|
Both parts: 173.084ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 36/50
|
Total stars: 38/50
|
||||||
Total time: 161581.562ms
|
Total time: 176482.246ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
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