Skip to content

Instantly share code, notes, and snippets.

@kirkegaard
Created December 3, 2024 18:14
Show Gist options
  • Save kirkegaard/0bee2dd0045937f92699ea016726ea1a to your computer and use it in GitHub Desktop.
Save kirkegaard/0bee2dd0045937f92699ea016726ea1a to your computer and use it in GitHub Desktop.
advent of code day 3
const input = await Deno.readTextFile("input.txt");
function matchMuls(str: string): string[] {
return str.match(/mul\(\d+,\d+\)/g) || [];
}
function calculateMulSum(arr: string[]): number {
return arr.reduce((acc, str) => {
const [a, b] = str.slice(4, -1).split(",").map(Number);
return acc + a * b;
}, 0);
}
function calculateTotal(input: string, excludeDont: boolean = false): number {
const filteredInput = excludeDont
? input.replace(/don't\(\).+?(do\(\)|$)/gs, "")
: input;
const mulMatches = matchMuls(filteredInput);
return calculateMulSum(mulMatches);
}
const total_1 = calculateTotal(input);
const total_2 = calculateTotal(input, true);
console.log({ total_1, total_2 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment