-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.mjs
More file actions
69 lines (57 loc) · 1.59 KB
/
Copy path14.mjs
File metadata and controls
69 lines (57 loc) · 1.59 KB
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
import { readInput } from "./utils.mjs";
const input = readInput(import.meta);
const tilt = (lines) => {
for (let i = 0; i < lines.length; i++) {
for (let j = 0; j < lines[i].length; j++) {
if (lines[i][j] === "O") {
let rI = i;
while (lines[rI - 1] && lines[rI - 1][j] === ".") {
rI -= 1;
}
lines[i][j] = ".";
lines[rI][j] = "O";
}
}
}
};
const rotate = (lines) =>
lines.map((_, i) => lines.map((_, j) => lines[lines.length - 1 - j][i]));
const performCycle = (lines) => {
for (let n = 0; n < 4; n++) {
tilt(lines);
lines = rotate(lines);
}
return lines;
};
const addWeights = (lines) => {
const totalLines = lines.length;
return lines.reduce(
(acc, line, i) =>
acc + line.filter((c) => c === "O").length * (totalLines - i),
0,
);
};
const solve1 = (input) => {
const lines = input.split("\n").map((line) => line.split(""));
tilt(lines);
return addWeights(lines);
};
const solve2 = (input) => {
const CYCLES = 1000000000;
const hashes = new Map();
let lines = input.split("\n").map((line) => line.split(""));
for (let i = 0; i < CYCLES; i++) {
const key = JSON.stringify(lines);
lines = performCycle(lines);
if (hashes.has(key)) {
const cycleLength = i - hashes.get(key);
const resultCacheI =
hashes.size - cycleLength + ((CYCLES - hashes.size) % cycleLength);
const resultLines = JSON.parse([...hashes.keys()][resultCacheI]);
return addWeights(resultLines);
}
hashes.set(key, i);
}
};
console.log(solve1(input));
console.log(solve2(input));