2022 day 9 and add utils
This commit is contained in:
parent
118fc14488
commit
cfd970e216
33
aoc_2022/day-09/example.test.ts
Normal file
33
aoc_2022/day-09/example.test.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { main as part1 } from "./part_1";
|
||||
import { main as part2 } from "./part_2";
|
||||
|
||||
const example = `R 4
|
||||
U 4
|
||||
L 3
|
||||
D 1
|
||||
R 4
|
||||
D 1
|
||||
L 5
|
||||
R 2`.split("\n");
|
||||
const example2 = `R 5
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20`.split("\n");
|
||||
//const example = `1 2 3 4 5`.split(" ");
|
||||
|
||||
test("part1", async () => {
|
||||
const answer = 13;
|
||||
const res = await part1(example);
|
||||
expect(res).toEqual(answer);
|
||||
});
|
||||
|
||||
test("part2", async () => {
|
||||
const answer = 36;
|
||||
const res = await part2(example2);
|
||||
expect(res).toEqual(answer);
|
||||
});
|
7
aoc_2022/day-09/logs/out_1.txt
Normal file
7
aoc_2022/day-09/logs/out_1.txt
Normal file
@ -0,0 +1,7 @@
|
||||
=== COMPUTATION ===
|
||||
|
||||
|
||||
=== /COMPUTATION ===
|
||||
|
||||
=== ANSWER TO P1 ===
|
||||
6269
|
7
aoc_2022/day-09/logs/out_2.txt
Normal file
7
aoc_2022/day-09/logs/out_2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
=== COMPUTATION ===
|
||||
|
||||
|
||||
=== /COMPUTATION ===
|
||||
|
||||
=== ANSWER TO P2 ===
|
||||
2557
|
75
aoc_2022/day-09/part_1.ts
Normal file
75
aoc_2022/day-09/part_1.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { JSONSet } from "@/utils";
|
||||
|
||||
type Point = {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
const distance = (p1: Point, p2: Point) =>
|
||||
Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
|
||||
|
||||
const isDiagAdj = (p1: Point, p2: Point) => distance(p1, p2) <= Math.sqrt(2);
|
||||
|
||||
export const main = async (lines: string[]): Promise<number | string> => {
|
||||
const knots: Point[] = Array(2)
|
||||
.fill(null)
|
||||
.map(() => ({ x: 0, y: 0 }));
|
||||
|
||||
const visited: JSONSet<Point> = new JSONSet();
|
||||
visited.add(knots[0]);
|
||||
|
||||
for (const step of lines) {
|
||||
const [dir, steps_s] = step.split(" ");
|
||||
const steps = parseInt(steps_s);
|
||||
|
||||
for (let i = 0; i < steps; ++i) {
|
||||
if (dir === "U") knots[0].y += 1;
|
||||
if (dir === "L") knots[0].x -= 1;
|
||||
if (dir === "R") knots[0].x += 1;
|
||||
if (dir === "D") knots[0].y -= 1;
|
||||
|
||||
for (let knotidx = 1; knotidx < knots.length; knotidx++) {
|
||||
const [head, tail] = [knots[knotidx - 1], knots[knotidx]];
|
||||
if (!isDiagAdj(head, tail)) {
|
||||
if (dir === "U") {
|
||||
tail.x = head.x;
|
||||
tail.y = head.y - 1;
|
||||
}
|
||||
if (dir === "L") {
|
||||
tail.x = head.x + 1;
|
||||
tail.y = head.y;
|
||||
}
|
||||
if (dir === "R") {
|
||||
tail.x = head.x - 1;
|
||||
tail.y = head.y;
|
||||
}
|
||||
if (dir === "D") {
|
||||
tail.x = head.x;
|
||||
tail.y = head.y + 1;
|
||||
}
|
||||
visited.add(tail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return visited.size;
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
|
||||
if (isrun) {
|
||||
const file = Bun.file("./problem.txt");
|
||||
const text = await file.text();
|
||||
const lines = text.split("\n").filter((x) => x && x.length);
|
||||
|
||||
console.log("=== COMPUTATION ===\n");
|
||||
|
||||
const answer = await main(lines);
|
||||
|
||||
console.log("\n=== /COMPUTATION ===\n");
|
||||
|
||||
console.log("=== ANSWER TO P1 ===");
|
||||
console.log(answer);
|
||||
}
|
61
aoc_2022/day-09/part_2.ts
Normal file
61
aoc_2022/day-09/part_2.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import { JSONSet, Vec2, l2Norm, isDiagAdj } from "@/utils";
|
||||
|
||||
export const main = async (lines: string[]): Promise<number | string> => {
|
||||
const knots: Vec2[] = Array(10)
|
||||
.fill(null)
|
||||
.map(() => ({ x: 0, y: 0 }));
|
||||
|
||||
const visited: JSONSet<Vec2> = new JSONSet();
|
||||
visited.add(knots[0]);
|
||||
|
||||
for (const step of lines) {
|
||||
const [dir, steps_s] = step.split(" ");
|
||||
const steps = parseInt(steps_s);
|
||||
|
||||
for (let i = 0; i < steps; ++i) {
|
||||
if (dir === "U") knots[0].y += 1;
|
||||
if (dir === "L") knots[0].x -= 1;
|
||||
if (dir === "R") knots[0].x += 1;
|
||||
if (dir === "D") knots[0].y -= 1;
|
||||
|
||||
for (let knotidx = 1; knotidx < knots.length; knotidx++) {
|
||||
const [head, tail] = [knots[knotidx - 1], knots[knotidx]];
|
||||
|
||||
if (!isDiagAdj(head, tail)) {
|
||||
if (head.y - tail.y === 0) {
|
||||
tail.x += Math.floor((head.x - tail.x) / 2);
|
||||
} else if (head.x - tail.x === 0) {
|
||||
tail.y += Math.floor((head.y - tail.y) / 2);
|
||||
} else {
|
||||
tail.x += head.x - tail.x > 0 ? 1 : -1;
|
||||
tail.y += head.y - tail.y > 0 ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (knotidx === knots.length - 1) {
|
||||
visited.add(tail);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return visited.size;
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
|
||||
if (isrun) {
|
||||
const file = Bun.file("./problem.txt");
|
||||
const text = await file.text();
|
||||
const lines = text.split("\n").filter((x) => x && x.length);
|
||||
|
||||
console.log("=== COMPUTATION ===\n");
|
||||
|
||||
const answer = await main(lines);
|
||||
|
||||
console.log("\n=== /COMPUTATION ===\n");
|
||||
|
||||
console.log("=== ANSWER TO P2 ===");
|
||||
console.log(answer);
|
||||
}
|
2000
aoc_2022/day-09/problem.txt
Normal file
2000
aoc_2022/day-09/problem.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -17,6 +17,9 @@
|
||||
"allowJs": true,
|
||||
"types": [
|
||||
"bun-types" // add Bun global
|
||||
]
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./*"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
utils/index.ts
Normal file
2
utils/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./jsonds";
|
||||
export * from "./point2d";
|
66
utils/jsonds.ts
Normal file
66
utils/jsonds.ts
Normal file
@ -0,0 +1,66 @@
|
||||
export class JSONSet<T extends Object> {
|
||||
private items: Set<string>;
|
||||
|
||||
constructor() {
|
||||
this.items = new Set<string>();
|
||||
}
|
||||
|
||||
add(item: T): void {
|
||||
const itemJson = JSON.stringify(item, Object.keys(item).sort());
|
||||
this.items.add(itemJson);
|
||||
}
|
||||
|
||||
has(item: T): boolean {
|
||||
const itemJson = JSON.stringify(item, Object.keys(item).sort());
|
||||
return this.items.has(itemJson);
|
||||
}
|
||||
|
||||
delete(item: T): boolean {
|
||||
const itemJson = JSON.stringify(item, Object.keys(item).sort());
|
||||
return this.items.delete(itemJson);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.items.clear();
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.items.size;
|
||||
}
|
||||
}
|
||||
|
||||
export class JSONHashMap<T extends Object> {
|
||||
private map: Map<string, T>;
|
||||
|
||||
constructor() {
|
||||
this.map = new Map<string, T>();
|
||||
}
|
||||
|
||||
set(key: T, value: T): void {
|
||||
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
||||
this.map.set(keyJson, value);
|
||||
}
|
||||
|
||||
get(key: T): T | undefined {
|
||||
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
||||
return this.map.get(keyJson);
|
||||
}
|
||||
|
||||
has(key: T): boolean {
|
||||
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
||||
return this.map.has(keyJson);
|
||||
}
|
||||
|
||||
delete(key: T): boolean {
|
||||
const keyJson = JSON.stringify(key, Object.keys(key).sort());
|
||||
return this.map.delete(keyJson);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.map.clear();
|
||||
}
|
||||
|
||||
get size(): number {
|
||||
return this.map.size;
|
||||
}
|
||||
}
|
9
utils/point2d.ts
Normal file
9
utils/point2d.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export type Vec2 = {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
export const l2Norm = (p1: Vec2, p2: Vec2) =>
|
||||
Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
|
||||
|
||||
export const isDiagAdj = (p1: Vec2, p2: Vec2) => l2Norm(p1, p2) <= Math.sqrt(2);
|
Loading…
Reference in New Issue
Block a user