Day 3
This commit is contained in:
29
2024/03/index.ts
Normal file
29
2024/03/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
const fs = require('fs');
|
||||
|
||||
let input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||||
|
||||
let result = 0;
|
||||
|
||||
const expressions: Array<RegExpMatchArray> = Array.from(input.matchAll(/(do(n't)?)\(\)|mul\((\d{1,3}),(\d{1,3})\)/g));
|
||||
|
||||
let enabled = true;
|
||||
|
||||
expressions.forEach((match) => {
|
||||
const expr = match[0];
|
||||
|
||||
// Part 2
|
||||
if (expr.startsWith('do(')) {
|
||||
enabled = true;
|
||||
return;
|
||||
}
|
||||
if (expr.startsWith('don\'t(')) {
|
||||
enabled = false;
|
||||
return
|
||||
}
|
||||
// End part 2
|
||||
if (enabled) {
|
||||
result += (parseInt(match[3]) * parseInt(match[4]));
|
||||
}
|
||||
});
|
||||
|
||||
console.log(result);
|
||||
Reference in New Issue
Block a user