This commit is contained in:
2024-12-02 14:28:09 +01:00
parent e6052f71c4
commit 22e0cea352
6 changed files with 1130 additions and 7 deletions

View File

@@ -1,7 +1,8 @@
import * as path from 'path'
import {sum, zipWith} from 'remeda'
export async function day1Part1() {
const input = await Bun.file(path.join(__dirname,'input.txt')).text()
const input = await Bun.file(path.join(__dirname, 'input.txt')).text()
const lines = input.split('\n')
const column1: number[] = []
const column2: number[] = []
@@ -17,12 +18,13 @@ export async function day1Part1() {
let result = 0
for(let i=0; i< column1.length; i++){
for (let i = 0; i < column1.length; i++) {
const left = column1[i]
const right = column2[i]
const diff = Math.abs(left - right)
result += diff
}
const foo = sum(zipWith(column1, column2, (a, b) => Math.abs(a - b)))
console.log({result})
console.log({result, foo})
}

43
day-2/day2.test.ts Normal file
View 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);
});

76
day-2/day2.ts Normal file
View File

@@ -0,0 +1,76 @@
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[]) {
let increasing = null;
let safe = true;
for (let i = 0; i < levels.length - 1; i++) {
const current = levels[i];
const next = levels[i + 1];
const diff = Math.abs(next - current);
if (diff > 3 || diff < 1) {
safe = false;
break;
}
if (i === 0) {
increasing = next > current;
continue;
}
if (increasing && next <= current) {
safe = false;
break;
}
if (!increasing && next >= current) {
safe = false;
break;
}
}
return safe;
}

1000
day-2/input.txt Normal file

File diff suppressed because it is too large Load Diff

6
day-2/test-input.txt Normal file
View 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

View File

@@ -1,4 +0,0 @@
import {day1Part1} from "./day-1/day1-part1.ts";
import {day1Part2} from "./day-1/day1-part2.ts";
await day1Part2()