-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday03_corrupted.ts
76 lines (66 loc) · 2.37 KB
/
day03_corrupted.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import assert from "node:assert";
import fs from "fs";
const parseInput = (input: string) => {
return input.trim().split("\n");
};
const findUncorrptedMemory = (
section: string,
conditionsEnabled: boolean = false,
) => {
let PATTERN: RegExp;
if (!conditionsEnabled) {
PATTERN = new RegExp("mul\\(\\d+,\\d+\\)", "g");
} else {
PATTERN = new RegExp("mul\\(\\d+,\\d+\\)|do\\(\\)|don\\'t\\(\\)", "g");
}
return Array.from(section.matchAll(PATTERN)).map(String);
};
const runMultiplicationInstruction = (instruction: string) => {
// instruction will look like mul(2,4)
if (!instruction.startsWith("mul")) throw Error("unexpected instruction");
const firstNum = Number(instruction.split("(")[1].split(",")[0]);
const secondNum = Number(instruction.split(",")[1].split(")")[0]);
return firstNum * secondNum;
};
assert(runMultiplicationInstruction("mul(2,4)") === 8);
assert(runMultiplicationInstruction("mul(5,5)") === 25);
assert(runMultiplicationInstruction("mul(11,8)") === 88);
assert(runMultiplicationInstruction("mul(8,5)") === 40);
const runProgram = (instructions: string[]) => {
let result = 0;
let instructionApplies = true;
instructions.forEach((instruction) => {
if (instruction.startsWith("do(")) {
instructionApplies = true;
return;
}
if (instruction.startsWith("don't")) {
instructionApplies = false;
return;
}
if (!instructionApplies) return;
result += runMultiplicationInstruction(instruction);
});
return result;
};
const sumUncorruptedMemory = (
memory: string[],
conditionsEnabled: boolean = false,
) => {
const instructions = memory.flatMap((section) =>
findUncorrptedMemory(section, conditionsEnabled),
);
return runProgram(instructions);
};
const TEST_INPUT =
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
const testMemory = parseInput(TEST_INPUT);
assert(sumUncorruptedMemory(testMemory) === 161);
const TEST_INPUT_PART2 =
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
const testMemoryPart2 = parseInput(TEST_INPUT_PART2);
assert(sumUncorruptedMemory(testMemoryPart2, true) === 48);
const puzzleInput = fs.readFileSync("data/day03_input.txt").toString();
const puzzleMemory = parseInput(puzzleInput);
console.log("Part 1:", sumUncorruptedMemory(puzzleMemory));
console.log("Part 2:", sumUncorruptedMemory(puzzleMemory, true));