30 lines
618 B
TypeScript
30 lines
618 B
TypeScript
|
|
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);
|