This commit is contained in:
2024-12-05 12:07:46 +01:00
parent 8d19b0405b
commit 40669eec80
4 changed files with 1528 additions and 0 deletions

38
day-5/day5.test.ts Normal file
View File

@@ -0,0 +1,38 @@
import { expect, test } from "bun:test";
import * as path from "node:path";
import { day5, day5part2 } from "./day5.ts";
test("day 5, 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 = day5(testInput);
const finalResult = day5(input);
console.log("\n\n");
console.log("Test data:", testResult);
console.log("Full data:", finalResult);
console.log("\n\n");
expect(testResult).toEqual(143);
expect(finalResult).toEqual(5588);
});
test("day 5, 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 = day5part2(testInput);
console.log("\n\n");
console.log("Test data:", testResult);
const finalResult = day5part2(input);
console.log("Full data:", finalResult);
expect(finalResult).toEqual(5331);
console.log("\n\n");
expect(testResult).toEqual(123);
});

89
day-5/day5.ts Normal file
View File

@@ -0,0 +1,89 @@
export function day5(input: string) {
let result = 0;
const [rulesStr, updatesStr] = input.split("\n\n");
const rules = rulesStr.split("\n").map((x) => x.split("|"));
const updates = updatesStr.split("\n").map((x) => x.split(","));
for (const update of updates) {
let isValid = true;
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
const x = rule[0];
const y = rule[1];
const xIndex = update.indexOf(x);
const yIndex = update.indexOf(y);
if (xIndex === -1 || yIndex === -1) {
continue;
}
if (yIndex < xIndex) {
console.log("not valid", {
x,
y,
xIndex,
yIndex,
update,
});
isValid = false;
break;
}
}
if (isValid) {
const middleValue = update[Math.floor(update.length / 2)];
result += Number.parseInt(middleValue);
}
}
return result;
}
export function day5part2(input: string) {
let result = 0;
const [rulesStr, updatesStr] = input.split("\n\n");
const rules = rulesStr.split("\n").map((x) => x.split("|"));
const updates = updatesStr.split("\n").map((x) => x.split(","));
const invalidUpdates = [];
for (const update of updates) {
let isValid = true;
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
const x = rule[0];
const y = rule[1];
const xIndex = update.indexOf(x);
const yIndex = update.indexOf(y);
if (xIndex === -1 || yIndex === -1) {
continue;
}
if (yIndex < xIndex) {
isValid = false;
break;
}
}
if (!isValid) {
invalidUpdates.push(update);
}
}
for (const update of invalidUpdates) {
update.sort((a, b) => {
const rule = rules.find((v) => v.includes(a) && v.includes(b));
if (!rule) {
return 0;
}
if (rule[0] === a) {
return -1;
}
return 1;
});
const middleValue = update[Math.floor(update.length / 2)];
result += Number.parseInt(middleValue);
}
return result;
}

1373
day-5/input.txt Normal file

File diff suppressed because it is too large Load Diff

28
day-5/test-input.txt Normal file
View File

@@ -0,0 +1,28 @@
47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13
75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47