Skip to content

Commit

Permalink
Year 2024, Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Dec 3, 2024
1 parent 8a18a8b commit 19d6e86
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions clients/typescript/solutions/S2403.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ISolution from "./ISolution.ts";

export default class S2403 implements ISolution {
firstPart(input: string): (number | string) | Promise<number | string> {
const matches = input.matchAll(/mul\((\d+),(\d+)\)/g);
return Array.from(matches).reduce((acc, match) => {
return acc + parseInt(match[1]) * parseInt(match[2]);
}, 0);
}
secondPart(input: string): (number | string) | Promise<number | string> {
const matches = input.matchAll(/(mul\((\d+),(\d+)\))|(do\(\))|(don't\(\))/g);
let en = true;
return Array.from(matches).reduce((acc, match) => {
const [instruct, _2, a, b] = match;
if (instruct === 'do()') en = true;
if (instruct === 'don\'t()') en = false;
if (instruct.startsWith('mul')) {
if (en) {
return acc + parseInt(a) * parseInt(b);
}
}
return acc;
}, 0);
}

}

0 comments on commit 19d6e86

Please sign in to comment.