-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.js
148 lines (118 loc) · 3.47 KB
/
part2.js
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
145
146
147
148
const { getInputString } = require("../utils/getInputString.js");
const BLOCKING_CHARS = ["#", "O"]
const fillBetween = (a, b) => {
const path = [a];
if(b < a) {
while(path[path.length -1] !== b) {
path.push(path[path.length -1]-1);
}
} else {
while(path[path.length -1] !== b) {
path.push(path[path.length -1]+1);
}
}
return path;
}
const getRockClusters = (file) => {
const clusters = []
for(const cluster of file.split('\n')) {
const points = cluster.split(' -> ');
const filledCluster = [];
for(let i = 1; i < points.length; i++) {
const [px, py] = points[i-1].split(',').map(val => parseInt(val));
const [x, y] = points[i].split(',').map(val => parseInt(val));
if(px === x) {
const toFill = fillBetween(py, y);
for(const ny of toFill) {
filledCluster.push([x, ny]);
}
} else if (py === y) {
const toFill = fillBetween(px, x);
for(const nx of toFill) {
filledCluster.push([nx, y]);
}
}
}
clusters.push(filledCluster)
}
return clusters;
}
const normalise = (sandStart, clusters) => {
const flatClusters = clusters.flatMap((x) => x);
const minX = Math.min(...[...flatClusters.map(([x, y]) => x), sandStart[0]]) - 250
const minY = Math.min(...[...flatClusters.map(([x, y]) => y), sandStart[1]])
for(const cluster of clusters) {
for(const point of cluster) {
point[0] = point[0] - minX;
point[1] = point[1] - minY;
}
}
sandStart[0] = sandStart[0] - minX;
sandStart[1] = sandStart[1] - minY;
globalSandStart = sandStart;
return {
sandStart,
clusters
}
}
let globalSandStart;
const createMap = (sandStart, clusters) => {
clusters = clusters.flatMap((x) => x);
const maxX = Math.max(...[...clusters.map(([x, y]) => x), sandStart[0]]) + 250
const maxY = Math.max(...[...clusters.map(([x, y]) => y), sandStart[1]]) + 2
const map = [...Array(maxY + 1)].map(e => Array(maxX + 1).fill('.'));
map[sandStart[1]][sandStart[0]] = "+";
for(const [x, y] of clusters) {
map[y][x] = '#'
}
for(const x in map[maxY]) {
map[maxY][x] = "#"
}
return map;
}
const draw = (screen) => {
for(const row of screen) {
console.log(row.join(''));
}
}
const getEndSandPosition = (sandPosition, map) => {
const positionsByPriority = [[0, 1], [-1, 1], [1, 1]];
const canMove = positionsByPriority.filter(([x, y]) => {
if(sandPosition[1] + y > -1 && sandPosition[1] + y < map.length) {
if(sandPosition[0] + x > -1 && sandPosition[0] + x < map[0].length) {
return !BLOCKING_CHARS.includes(map[sandPosition[1] + y][sandPosition[0] + x])
}
}
return false;
});
if(canMove.length === 0) {
if(sandPosition == globalSandStart) {
throw new Error("overflow");
}
return sandPosition
} else {
for(const [x, y] of canMove) {
if(!BLOCKING_CHARS.includes(map[sandPosition[1] + y][sandPosition[0] + x])) {
return getEndSandPosition([sandPosition[0] + x, sandPosition[1] + y], map);
}
}
}
}
const Run = () => {
const file = getInputString(14, false);
const {sandStart, clusters} = normalise([500, 0], getRockClusters(file))
const map = createMap(sandStart, clusters);
let i = 0;
while(i >= 0) {
i++
try {
const endPosition = getEndSandPosition(sandStart, map);
map[endPosition[1]][endPosition[0]] = "O"
} catch(e) {
console.log(`overflow at ${i}`)
break;
}
}
// draw(map)
}
Run();