This commit is contained in:
Tyler Beckman 2024-12-02 03:22:49 -07:00
parent 7bdeb7864b
commit fb9a36535b
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 46 additions and 0 deletions

17
day2/part1.ts Normal file
View file

@ -0,0 +1,17 @@
export async function solve() {
return await Deno.readTextFile("./input.txt").then((f) =>
f.split("\n").filter((l) => {
const report = l.split(" ").map((s) => +s);
const increasing = report[0] < report[1];
let last = report[0];
for (const number of report.slice(1)) {
if (number > last && !increasing) return false;
if (number < last && increasing) return false;
if (Math.abs(number - last) < 1 || Math.abs(number - last) > 3) return false;
last = number;
}
return true;
}).reduce(acc => acc + 1, 0)
);
}

29
day2/part2.ts Normal file
View file

@ -0,0 +1,29 @@
export async function solve() {
return await Deno.readTextFile("./input.txt").then((f) =>
f.split("\n").filter((l) => {
const report = l.split(" ").map((s) => +s);
for (let i = 0; i < report.length; i++) {
const sliced = [
...report.slice(0, i),
...report.slice(i + 1, report.length),
];
const increasing = sliced[0] < sliced[1];
let last = sliced[0];
let failed = false;
for (const number of sliced.slice(1)) {
if (number > last && !increasing) failed = true;
if (number < last && increasing) failed = true;
if (
Math.abs(number - last) < 1 ||
Math.abs(number - last) > 3
) failed = true;
last = number;
}
if (failed) continue;
else return true;
}
return false;
}).reduce((acc) => acc + 1, 0)
);
}