Compare commits
3 commits
ee66a68efb
...
7acfe9c402
Author | SHA1 | Date | |
---|---|---|---|
7acfe9c402 | |||
7df462c08f | |||
763be9b589 |
2 changed files with 78 additions and 0 deletions
38
day2.ts
Normal file
38
day2.ts
Normal 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
40
day3.ts
Normal 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());
|
Loading…
Add table
Reference in a new issue