advent of code 2025 in ts and nix

feat: add part two of day 1

dunkirk.sh fbfecad0 15723815

verified
Changed files
+56 -11
ts
+46 -11
ts/01/index.ts
···
const file = await Bun.file("./input.txt").text();
-
let dial = 50;
-
let count = 0;
+
(() => {
+
let dial = 50;
+
let count = 0;
+
+
file.split("\n").forEach((line) => {
+
const dir = line.startsWith("R"); // false is left
+
const num = Number.parseInt(line.substring(1), 10);
+
+
if (dir) dial += num;
+
else dial -= num;
+
+
dial %= 100;
+
+
if (dial === 0) count++;
+
});
+
+
console.log(`part 1: ${count}`);
+
})();
+
+
(() => {
+
let dial = 50;
+
let count = 0;
+
+
file.split("\n").forEach((line) => {
+
line = line.trim();
+
const dir = line[0]; // false is left
+
const num = Number.parseInt(line.substring(1), 10);
+
+
const dialBefore = dial;
-
file.split("\n").forEach((line) => {
-
const dir = line.startsWith("R"); // false is left
-
const num = Number.parseInt(line.substring(1), 10);
+
let distToZero: number;
+
if (dir === "R") {
+
distToZero = (100 - dialBefore) % 100;
+
} else {
+
distToZero = dialBefore % 100;
+
}
-
if (dir) dial -= num;
-
else dial += num;
+
if (distToZero === 0) distToZero = 100;
-
dial %= 100;
+
if (num >= distToZero) {
+
count += 1 + Math.floor((num - distToZero) / 100);
+
}
-
if (dial === 0) count++;
-
});
+
if (dir === "R") dial = (dialBefore + num) % 100;
+
else dial = (100 + dialBefore - (num % 100)) % 100;
+
+
dial = (100 + dial) % 100;
+
});
-
console.log(`part 1: ${count}`);
+
console.log(`part 2: ${count}`);
+
})();
+10
ts/01/test_input.txt
···
+
L68
+
L30
+
R48
+
L5
+
R60
+
L55
+
L1
+
L99
+
R14
+
L82