mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2026-02-01 12:34:30 +00:00
move things around a bit
This commit is contained in:
43
2024/day-2/day2.test.ts
Normal file
43
2024/day-2/day2.test.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import * as path from "node:path";
|
||||
import { day2, day2part2 } from "./day2.ts";
|
||||
|
||||
test("day 2, part 1", async () => {
|
||||
const testInput = await Bun.file(
|
||||
path.resolve(__dirname, "test-input.txt"),
|
||||
).text();
|
||||
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||
|
||||
const [testResult, finalResult] = await Promise.all([
|
||||
day2(testInput),
|
||||
day2(input),
|
||||
]);
|
||||
|
||||
console.log("\n\n");
|
||||
console.log("Test data:", testResult);
|
||||
console.log("Full data:", finalResult);
|
||||
console.log("\n\n");
|
||||
|
||||
expect(testResult).toEqual(2);
|
||||
expect(finalResult).toEqual(359);
|
||||
});
|
||||
|
||||
test("day 2, part 2", async () => {
|
||||
const testInput = await Bun.file(
|
||||
path.resolve(__dirname, "test-input.txt"),
|
||||
).text();
|
||||
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
|
||||
|
||||
const [testResult, finalResult] = await Promise.all([
|
||||
day2part2(testInput),
|
||||
day2part2(input),
|
||||
]);
|
||||
|
||||
console.log("\n\n");
|
||||
console.log("Test data:", testResult);
|
||||
console.log("Full data:", finalResult);
|
||||
console.log("\n\n");
|
||||
|
||||
expect(testResult).toEqual(4);
|
||||
expect(finalResult).toEqual(418);
|
||||
});
|
||||
55
2024/day-2/day2.ts
Normal file
55
2024/day-2/day2.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import * as R from "remeda";
|
||||
|
||||
export async function day2(input: string) {
|
||||
const lines = input.split("\n");
|
||||
let count = 0;
|
||||
for (const line of lines) {
|
||||
const levels = line.split(" ").map((v) => Number.parseInt(v, 10));
|
||||
const safe = isSafe(levels);
|
||||
|
||||
if (safe) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
export async function day2part2(input: string) {
|
||||
const lines = input.split("\n");
|
||||
let count = 0;
|
||||
for (const line of lines) {
|
||||
const levels = line.split(" ").map((v) => Number.parseInt(v, 10));
|
||||
const isFullySafe = isSafe(levels);
|
||||
|
||||
if (isFullySafe) {
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
let isDampenedSafe = false;
|
||||
|
||||
for (let i = 0; i < levels.length; i++) {
|
||||
const toTry = levels.toSpliced(i, 1);
|
||||
|
||||
if (isSafe(toTry)) {
|
||||
isDampenedSafe = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isDampenedSafe) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
function isSafe(levels: number[]) {
|
||||
const diffs = R.zip(levels, levels.slice(1)).map(([a, b]) => a - b);
|
||||
|
||||
return (
|
||||
diffs.every((d) => 1 <= d && d <= 3) ||
|
||||
diffs.every((d) => -1 >= d && d >= -3)
|
||||
);
|
||||
}
|
||||
1000
2024/day-2/input.txt
Normal file
1000
2024/day-2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2024/day-2/test-input.txt
Normal file
6
2024/day-2/test-input.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
Reference in New Issue
Block a user