-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.js
158 lines (147 loc) · 3.76 KB
/
24.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
149
150
151
152
153
154
155
156
157
158
// Total duration: 35 min (rounded to the nearest minute)
// Started at: 9:13
// First star at: 9:38
// Second star at: 9:48
const solveProblem = (data) => {
// Parse data
const lines = data.split("\n").slice(0, -1);
const blizzards = [];
let possiblePositions = [{i: 0, j: 1, reachedGoal: false, reachedStart: false}];
let height = lines.length - 2;
let width = lines[0].length - 2;
lines.forEach((line, i) => {
if (i == 0 || i == lines.length - 1) {
return;
}
line.split("").forEach((char, j) => {
let dir = {">": 0, "v": 1, "<": 2, "^": 3}[char];
if (dir !== undefined) {
blizzards.push({i, j, dir})
}
})
})
// Part 1
const simulateBlizzards = () => {
blizzards.forEach((b) => {
const {dir} = b;
switch (dir) {
case 0:
b.j++;
if (b.j == width + 1) b.j = 1;
break;
case 1:
b.i++;
if (b.i == height + 1) b.i = 1;
break;
case 2:
b.j--;
if (b.j == 0) b.j = width;
break;
case 3:
b.i--;
if (b.i == 0) b.i = height;
break;
}
})
}
const isAvailable = (i, j) => j > 0 && j <= width && (i > 0 || (i == 0 && j == 1)) && (i <= height || (i == height + 1 && j == width)) && !blizzards.some(b => b.i == i && b.j == j);
const moveElf = () => {
const newPossiblePositions = [];
const addIfAvailable = (i, j, reachedGoal, reachedStart) => {
const rG = reachedGoal || (i == height + 1);
const rS = reachedStart || (reachedGoal && i == 0);
if (isAvailable(i, j) && !newPossiblePositions.some(p => p.i == i && p.j == j && p.reachedGoal == rG && p.reachedStart == rS)) {
newPossiblePositions.push({
i,
j,
reachedGoal: rG,
reachedStart: rS,
});
}
}
possiblePositions.forEach(({i, j, reachedGoal, reachedStart}) => {
addIfAvailable(i, j, reachedGoal, reachedStart);
addIfAvailable(i, j + 1, reachedGoal, reachedStart);
addIfAvailable(i, j - 1, reachedGoal, reachedStart);
addIfAvailable(i + 1, j, reachedGoal, reachedStart);
addIfAvailable(i - 1, j, reachedGoal, reachedStart);
})
possiblePositions = newPossiblePositions;
}
const print = () => {
let r = "";
for (let i = 0; i <= height + 1; i++) {
r += "#";
for (let j = 1; j <= width; j++) {
const p = possiblePositions.find(p => p.i == i && p.j == j)
if (p) {
r += p.reachedGoal ? "G" : "E";
continue;
}
if (i == 0) {
if (j == 1) {
r += ".";
} else {
r += "#";
}
continue
}
if (i == height + 1) {
if (j == width) {
r += ".";
} else {
r += "#";
}
continue
}
const bs = blizzards.filter(b => b.i == i && b.j == j);
if (bs.length == 0) {
r += ".";
} else if (bs.length > 1) {
r += bs.length;
} else {
r += [">", "v", "<", "^"][bs[0].dir]
}
}
r += "#\n";
}
console.log(r);
}
// print();
let i = 0;
let part1Solved = false;
while (true) {
simulateBlizzards();
moveElf();
// console.log(i),
// console.log("all:", possiblePositions.length);
// console.log("reachedGoal:", possiblePositions.filter(p => p.reachedGoal).length);
// console.log("reachedStart:", possiblePositions.filter(p => p.reachedStart).length);
// console.log();
// print();
i++;
if (!part1Solved && possiblePositions.some(p => p.i == height + 1)) {
part1Solved = true;
console.log("Part 1: ", i);
}
if (possiblePositions.some(p => p.i == height + 1 && p.reachedStart)) {
break;
}
}
console.log("Part 2: ", i);
console.log("");
};
// Library code
const fs = require("fs");
const path = require('path');
const day = path.parse(__filename).name;
const files = [
`${day}-test`,
`${day}`,
];
files.forEach(file => {
fs.readFile(`./${file}.txt`, "utf8", (_, data) => {
console.log(`Data from file ${file}.txt`);
solveProblem(data);
});
});