Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 19, 2024
1 parent 102aa47 commit bff4cb7
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/2024/day19.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function countMatches(line, patterns, memo = {}) {
function countMatches(line, patterns, memo) {
let matches = 0;
if (line in memo) return memo[line];
if (line.length === 0) return 1;
Expand All @@ -7,26 +7,24 @@ function countMatches(line, patterns, memo = {}) {
matches += countMatches(line.slice(pattern.length), patterns, memo);
}
}
memo[line] = matches;
return matches;
return (memo[line] = matches);
}

export function part1(input) {
function solve(input) {
let [patterns, lines] = input.split("\n\n");
patterns = patterns.split(", ");
let matches = 0;
let matches = [];
let memo = {};
for (let line of lines.split("\n")) {
if (countMatches(line, patterns)) matches++;
matches.push(countMatches(line, patterns, memo));
}
return matches;
}

export function part1(input) {
return solve(input).filter(x => x > 0).length;
}

export function part2(input) {
let [patterns, lines] = input.split("\n\n");
patterns = patterns.split(", ");
let matches = 0;
for (let line of lines.split("\n")) {
matches += countMatches(line, patterns);
}
return matches;
return solve(input).reduce((a, b) => a + b);
}

0 comments on commit bff4cb7

Please sign in to comment.