-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.ts
144 lines (118 loc) · 3.12 KB
/
b.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
type Point = [number, number];
type Grid = ("#" | "." | "o" | "+")[][];
const _printGrid = (grid: Grid) => {
const canvas = grid.map((line) => line.slice(450, 550).join("")).join("\n");
console.log(canvas);
};
const findMaxXY = (lines: Point[][]) => {
let maxX = 0;
let maxY = 0;
lines.forEach((lines) => {
lines.forEach(([x, y]) => {
if (x > maxX) {
maxX = x;
}
if (y > maxY) {
maxY = y;
}
});
});
return [maxX, maxY];
};
const fillGrid = (lines: Point[][], sandSpawn: Point): Grid => {
const [maxScanX, maxScanY] = findMaxXY(lines);
const [maxX, maxY] = [maxScanX * maxScanX, maxScanY + 2];
lines.push([[0, maxY], [maxX, maxY]]);
const grid: ("#" | "." | "o" | "+")[][] = Array.from(
{ length: maxY + 1 },
() => Array.from({ length: maxX + 1 }, () => "."),
);
lines.forEach((line) => {
let prevPoint: null | Point = null;
line.forEach((point) => {
const [x, y] = point;
if (prevPoint === null) {
prevPoint = point;
grid[y][x] = "#";
return;
}
const d = [prevPoint[0] - point[0], prevPoint[1] - point[1]];
const pos = d[0] ? 0 : 1; // pos = 0 | d = [2,0]
for (let xy = prevPoint[pos]; xy != point[pos];) {
if (d[pos] < 0) {
xy++;
} else {
xy--;
}
grid[pos === 1 ? xy : y][pos === 0 ? xy : x] = "#";
}
prevPoint = point;
});
});
grid[sandSpawn[1]][sandSpawn[0]] = "+";
return grid;
};
function solution(input: string) {
const lines = input
.split("\n")
.map((line) =>
line.split(" -> ").map((str) =>
str.split(",").map((p) => Number(p)) as Point
)
);
const sandSpawn: Point = [500, 0];
const grid = fillGrid(lines, sandSpawn);
let stable = 0;
let sand: null | Point = null;
while (true) {
if (!sand) {
sand = sandSpawn.slice() as Point;
}
while (true) {
let next = () => grid?.[sand![1] + 1]?.[sand![0]];
if (!next()) {
return stable;
} else if (next() === ".") {
sand[1]++;
continue;
}
next = () => grid?.[sand![1] + 1]?.[sand![0] - 1];
if (!next()) {
return stable;
} else if (next() === ".") {
sand[0]--;
sand[1]++;
continue;
}
next = () => grid?.[sand![1] + 1]?.[sand![0] + 1];
if (!next()) {
return stable;
} else if (next() === ".") {
sand[0]++;
sand[1]++;
continue;
}
break;
}
if (grid[sand[1]][sand[0]] === "+") {
stable++;
return stable;
}
grid[sand[1]][sand[0]] = "o";
stable++;
sand = null;
}
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./14/example.txt");
const actual = solution(input);
const expected = 93;
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./14/input.txt");
const actual = solution(input);
const expected = 24958;
assertEquals(actual, expected);
});