Skip to content

Commit

Permalink
Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Av32000 committed Dec 3, 2024
1 parent 0086d9d commit 6b97d29
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 03/part1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString();

// Apply Regex
const regex = /mul\((\d+),\s*(\d+)\)/g;
let match,
groups = [];
while ((match = regex.exec(input)) !== null) {
groups.push({ x: parseInt(match[1]), y: parseInt(match[2]) });
}

// Compute Result
let result = 0;
groups.forEach((g) => {
result += g.x * g.y;
});

console.log(result);
37 changes: 37 additions & 0 deletions 03/part2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString();

// Apply Regex
const regex = /mul\((\d+),\s*(\d+)\)|do\(\)|don't\(\)/g;
let match,
op = [];
while ((match = regex.exec(input)) !== null) {
if (match[1] && match[2]) {
op.push({ type: "mul", x: parseInt(match[1]), y: parseInt(match[2]) });
} else if (match[0] === "do()") {
op.push({ type: "do" });
} else if (match[0] === "don't()") {
op.push({ type: "don't" });
}
}

// Compute Result
let result = 0;
let enabled = true;
op.forEach((g) => {
switch (g.type) {
case "mul":
if (enabled) result += g.x * g.y;
break;
case "do":
enabled = true;
break;
case "don't":
enabled = false;
break;
}
});

console.log(result);

0 comments on commit 6b97d29

Please sign in to comment.