Skip to content

Commit

Permalink
Add day 19 of year 2024 🥛
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Dec 20, 2024
1 parent 309ca8c commit 47b7397
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 2024/days/day-19/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Benchmark

```
Platform: darwin arm64
CPU: Apple M3 Pro 11 Cores
Memory: 18.00 GB
```

## Sample 1

| part | time (~) | μs |
| ---- | -------- | ------------------- |
| 1 | 0.16ms | 0.1602500000000049 |
| 2 | 0.03ms | 0.03212500000000773 |

## Answer

| part | time (~) | μs |
| ---- | -------- | --------- |
| 1 | 74.96ms | 74.959875 |
| 2 | 51.48ms | 51.477959 |
10 changes: 10 additions & 0 deletions 2024/days/day-19/in.sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
r, wr, b, g, bwu, rb, gb, br

brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
52 changes: 52 additions & 0 deletions 2024/days/day-19/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function parse(source) {
const [patterns, designs] = source
.trim()
.split('\n\n')
.map((e) => e.match(/(\w+)/g));

return { patterns, designs };
}

function createPatternsCounter(patterns) {
const memo = new Map();

return (design) => {
const count = (str) => {
if (!str.length) {
return 1;
}

if (memo.has(str)) {
return memo.get(str);
}

let localCount = 0;
for (const pattern of patterns) {
if (str.startsWith(pattern)) {
const substr = str.slice(pattern.length);
localCount += count(substr);
}
}

memo.set(str, localCount);

return localCount;
};

return count(design);
};
}

function part1(data) {
const countWaysToCreateDesign = createPatternsCounter(data.patterns);
return data.designs.filter(countWaysToCreateDesign).length;
}

function part2(data) {
const countWaysToCreateDesign = createPatternsCounter(data.patterns);
return data.designs
.map(countWaysToCreateDesign)
.reduce((sum, e) => sum + e, 0);
}

module.exports = { parse, part1, part2 };

0 comments on commit 47b7397

Please sign in to comment.