Created
December 3, 2024 18:14
-
-
Save kirkegaard/0bee2dd0045937f92699ea016726ea1a to your computer and use it in GitHub Desktop.
advent of code day 3
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
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