-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.js
50 lines (38 loc) · 955 Bytes
/
day25.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
const fs = require("fs");
const array = fs
.readFileSync("day25.txt", { encoding: "utf-8" })
.split("\n")
.map((x) => x.split(""));
const dir = [
[">", 0, 1],
["v", 1, 0],
];
const myFunction = (array) => {
const height = array.length;
const width = array[0].length;
let steps = 0;
let moved = false;
do {
steps++;
moved = false;
for (let i = 0; i < 2; i++) {
let g = array.map((x) => x.map((y) => y));
for (let j = 0; j < height; j++) {
for (let k = 0; k < width; k++) {
if (
g[j][k] === dir[i][0] &&
g[(j + dir[i][1]) % height][(k + dir[i][2]) % width] === "."
) {
moved = true;
array[(j + dir[i][1]) % height][(k + dir[i][2]) % width] =
dir[i][0];
array[j][k] = ".";
}
}
}
}
} while (moved);
return steps;
};
const value = myFunction(array);
console.log(value);