-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add solutions for 2024 day 19
- Loading branch information
1 parent
531a486
commit 6f895c3
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { solution } from '../../utils/index.js'; | ||
|
||
const parse = (input) => { | ||
const parts = input.trim().split('\n\n'); | ||
|
||
const available = parts[0].split(', '); | ||
const desired = parts[1].split('\n'); | ||
|
||
return { available, desired }; | ||
}; | ||
|
||
// Calculates whether a given design is possible and returns how many variations | ||
const build = (design, available, cache = new Map()) => { | ||
if (cache.has(design)) { | ||
return cache.get(design); | ||
} | ||
|
||
let count = 0; | ||
for (const pattern of available) { | ||
if (design.startsWith(pattern)) { | ||
const remaining = design.slice(pattern.length); | ||
|
||
if (!remaining.length) { | ||
count += 1; | ||
continue; | ||
} | ||
|
||
count += build(remaining, available, cache); | ||
} | ||
} | ||
cache.set(design, count); | ||
return count; | ||
}; | ||
|
||
export const partOne = solution((input) => { | ||
const { available, desired } = parse(input); | ||
return desired.reduce((sum, design) => { | ||
if (build(design, available)) { | ||
return sum + 1; | ||
} | ||
return sum; | ||
}, 0); | ||
}); | ||
|
||
export const partTwo = solution((input) => { | ||
const { available, desired } = parse(input); | ||
return desired.reduce((sum, design) => sum + build(design, available), 0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
part-one: | ||
- input: &example1 | | ||
r, wr, b, g, bwu, rb, gb, br | ||
|
||
brwrr | ||
bggr | ||
gbbr | ||
rrbgbr | ||
ubwu | ||
bwurrg | ||
brgr | ||
bbrgwb | ||
answer: 6 | ||
|
||
part-two: | ||
- input: *example1 | ||
answer: 16 |