Compare commits

...

3 commits

Author SHA1 Message Date
7acfe9c402
day 3 2024-12-04 14:16:37 +11:00
7df462c08f
day 2 part 2 very late 2024-12-04 13:49:33 +11:00
763be9b589
Day 2 Puzzle 1 2024-12-02 22:49:37 +11:00
2 changed files with 78 additions and 0 deletions

38
day2.ts Normal file
View file

@ -0,0 +1,38 @@
import fs from "node:fs";
const input = fs.readFileSync("inputs/day2").toString();
const rows = input.split("\n").filter(Boolean);
function safe_levels(_row: number[]) {
const row = [..._row];
while (row.length > 1) {
const diff = row[1] - row[0];
if (diff > 3 || diff < 1) return false;
row.shift();
}
return true;
}
function puzzle1() {
return rows.map((row) => row.split(/\s+/g).map(i => +i))
.filter(row => safe_levels(row) || safe_levels(row.toReversed())).length;
}
console.log("Puzzle 1", puzzle1());
function dampen_lists(row: number[]) {
const rows: number[][] = new Array(row.length).fill(row).map((_row, i) => {
const row = [..._row];
row.splice(i, 1);
return row;
});
return [row, ...rows].filter(safe_levels).length > 0;
}
function puzzle2() {
return rows.map((row) => row.split(/\s+/g).map(i => +i))
.filter(row => dampen_lists(row) || dampen_lists(row.toReversed())).length;
}
console.log("Puzzle 2", puzzle2());

40
day3.ts Normal file
View file

@ -0,0 +1,40 @@
import fs from "node:fs";
const input = fs.readFileSync("inputs/day3").toString();
function puzzle1() {
const mul_regex = /mul\(([0-9]{1,3}),([0-9]{1,3})\)/g;
let sum = 0;
let match: RegExpExecArray | null = null;
while ((match = mul_regex.exec(input)) !== null) {
sum += +match[1] * + +match[2];
}
return sum;
}
console.log("Puzzle 1", puzzle1());
function puzzle2() {
const toggle_regex = /(?:do|don't)\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)/g;
let enabled = true;
let sum = 0;
let match: RegExpExecArray | null = null;
while ((match = toggle_regex.exec(input)) !== null) {
switch (match[0]) {
case "do()":
enabled = true;
break;
case "don't()":
enabled = false;
break;
default:
if (!enabled) break;
sum += +match[1] * + +match[2];
break;
}
}
return sum;
}
console.log("Puzzle 2", puzzle2());