Skip to content

Commit 40674fb

Browse files
2024 Day 19 complete
1 parent 6b6cc8d commit 40674fb

File tree

4 files changed

+148
-13
lines changed

4 files changed

+148
-13
lines changed

aoc2024/.aocrunner.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,18 @@
273273
},
274274
{
275275
"part1": {
276-
"solved": false,
277-
"result": null,
278-
"attempts": [],
279-
"time": null
276+
"solved": true,
277+
"result": "298",
278+
"attempts": [
279+
"296"
280+
],
281+
"time": 50.2825
280282
},
281283
"part2": {
282-
"solved": false,
283-
"result": null,
284+
"solved": true,
285+
"result": "572248688842069",
284286
"attempts": [],
285-
"time": null
287+
"time": 182.9422
286288
}
287289
},
288290
{

aoc2024/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
[![Day](https://badgen.net/badge/16/%E2%98%85%E2%98%85/green)](src/day16)
3030
[![Day](https://badgen.net/badge/17/%E2%98%85%E2%98%85/green)](src/day17)
3131
[![Day](https://badgen.net/badge/18/%E2%98%85%E2%98%85/green)](src/day18)
32-
![Day](https://badgen.net/badge/19/%E2%98%86%E2%98%86/gray)
32+
[![Day](https://badgen.net/badge/19/%E2%98%85%E2%98%85/green)](src/day19)
3333
![Day](https://badgen.net/badge/20/%E2%98%86%E2%98%86/gray)
3434
![Day](https://badgen.net/badge/21/%E2%98%86%E2%98%86/gray)
3535
![Day](https://badgen.net/badge/22/%E2%98%86%E2%98%86/gray)
@@ -195,9 +195,9 @@ Both parts: 16.593ms
195195

196196
```
197197
Day 19
198-
Time part 1: -
199-
Time part 2: -
200-
Both parts: -
198+
Time part 1: 50.282ms
199+
Time part 2: 182.942ms
200+
Both parts: 233.225ms
201201
```
202202

203203
```
@@ -243,8 +243,8 @@ Both parts: -
243243
```
244244

245245
```
246-
Total stars: 35/50
247-
Total time: 2434.8ms
246+
Total stars: 37/50
247+
Total time: 2668.025ms
248248
```
249249

250250
<!--/RESULTS-->

aoc2024/src/day19/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 🎄 Advent of Code 2024 - day 19 🎄
2+
3+
## Info
4+
5+
Task description: [link](https://adventofcode.com/2024/day/19)
6+
7+
## Notes
8+
9+
...

aoc2024/src/day19/index.ts

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import run from "aocrunner";
2+
3+
const parseInput = (rawInput: string) => {
4+
const [towelsStr, patternsStr] = rawInput.split("\n\n");
5+
const towels = towelsStr.split(", ");
6+
const patterns = patternsStr.split("\n");
7+
8+
return { towels, patterns };
9+
};
10+
11+
let cache: Record<string, boolean> = {};
12+
13+
const canMakePattern = (
14+
towels: string[],
15+
pattern: string,
16+
workingIndex: number
17+
) => {
18+
if (workingIndex >= pattern.length) {
19+
return true;
20+
}
21+
22+
if (cache[pattern.slice(workingIndex)] != undefined) {
23+
return cache[pattern.slice(workingIndex)];
24+
}
25+
26+
for (let i = 0; i < towels.length; i++) {
27+
if (
28+
pattern.slice(workingIndex, workingIndex + towels[i].length) === towels[i]
29+
) {
30+
if (canMakePattern(towels, pattern, workingIndex + towels[i].length)) {
31+
cache[pattern.slice(workingIndex)] = true;
32+
return true;
33+
}
34+
}
35+
}
36+
37+
cache[pattern.slice(workingIndex)] = false;
38+
return false;
39+
};
40+
41+
const part1 = (rawInput: string) => {
42+
const { patterns, towels } = parseInput(rawInput);
43+
44+
return patterns
45+
.map((p, i, a) => {
46+
cache = {};
47+
return canMakePattern(towels, p, 0) ? 1 : 0;
48+
})
49+
.reduce((prev, curr) => prev + curr, 0);
50+
};
51+
52+
let waysCache: Record<string, number> = {};
53+
54+
const waysToMakePattern = (
55+
towels: string[],
56+
pattern: string,
57+
workingIndex: number
58+
) => {
59+
if (waysCache[pattern.slice(workingIndex)] != undefined) {
60+
return waysCache[pattern.slice(workingIndex)];
61+
}
62+
63+
let ways = 0;
64+
65+
for (let i = 0; i < towels.length; i++) {
66+
if (pattern.slice(workingIndex) === towels[i]) {
67+
ways += 1;
68+
} else if (
69+
pattern.slice(workingIndex, workingIndex + towels[i].length) === towels[i]
70+
) {
71+
ways += waysToMakePattern(
72+
towels,
73+
pattern,
74+
workingIndex + towels[i].length
75+
);
76+
}
77+
}
78+
79+
waysCache[pattern.slice(workingIndex)] = ways;
80+
return ways;
81+
};
82+
83+
const part2 = (rawInput: string) => {
84+
const { patterns, towels } = parseInput(rawInput);
85+
86+
return patterns
87+
.map((p, i, a) => {
88+
waysCache = {};
89+
return waysToMakePattern(towels, p, 0);
90+
})
91+
.reduce((prev, curr) => prev + curr, 0);
92+
};
93+
94+
run({
95+
part1: {
96+
tests: [
97+
{
98+
input: `r, wr, b, g, bwu, rb, gb, br
99+
100+
brwrr
101+
bggr
102+
gbbr
103+
rrbgbr
104+
ubwu
105+
bwurrg
106+
brgr
107+
bbrgwb`,
108+
expected: 6,
109+
},
110+
],
111+
solution: part1,
112+
},
113+
part2: {
114+
tests: [
115+
// {
116+
// input: ``,
117+
// expected: "",
118+
// },
119+
],
120+
solution: part2,
121+
},
122+
trimTestInputs: true,
123+
onlyTests: false,
124+
});

0 commit comments

Comments
 (0)