mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-16 20:49:24 +00:00
day 2
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
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()
|
||||
@@ -23,6 +24,7 @@ export async function day1Part1() {
|
||||
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
43
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);
|
||||
});
|
||||
76
day-2/day2.ts
Normal file
76
day-2/day2.ts
Normal 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
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
6
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