-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.ts
59 lines (50 loc) · 1.59 KB
/
a.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts";
type Point = { x: number; y: number; z: number };
type Direction = { x: 0 | 1 | -1; y: 0 | 1 | -1; z: 0 | 1 | -1 };
const dirs: Direction[] = [
{ x: 0, y: 0, z: 1 },
{ x: 0, y: 0, z: -1 },
{ x: 0, y: 1, z: 0 },
{ x: 0, y: -1, z: 0 },
{ x: 1, y: 0, z: 0 },
{ x: -1, y: 0, z: 0 },
];
const calculateCartesian = (a: Point[], b: Point[]) => {
const setOfPoints = [];
for (const ac of a) {
for (const bc of b) {
const point = { x: ac.x + bc.x, y: ac.y + bc.y, z: ac.z + bc.z };
setOfPoints.push(point);
}
}
return setOfPoints;
};
const hashPoint = (point: Point) => `${point.x},${point.y},${point.z}`;
function solution(input: string): number {
const pointsMap = input
.split("\n")
.map((line) => line.split(",").map((a) => Number(a)))
.reduce((acc, [x, y, z]) => {
const point = { x, y, z };
const key = hashPoint(point);
acc[key] = point;
return acc;
}, {} as Record<string, Point>);
const pointsMapValues = Object.values(pointsMap);
return calculateCartesian(pointsMapValues, dirs).reduce(
(acc, p) => acc + (hashPoint(p) in pointsMap ? 0 : 1),
0,
);
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./18/example.txt");
const actual = solution(input);
const expected = 64;
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./18/input.txt");
const actual = solution(input);
const expected = 4450;
assertEquals(actual, expected);
});