-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwentyfive.js
82 lines (71 loc) · 1.76 KB
/
twentyfive.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
import { readFile } from 'fs/promises';
let grid = [];
// 25
async function init() {
const input = await readFile('twentyfive.txt', 'utf8');
grid = input.split('\n').map((line) => line.split(''));
// console.log(grid.join('\n'));
let i = 0;
while (true) {
let movers = 0;
let nextGrid = freshGrid(grid);
// check rights
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[0].length; x++) {
if (grid[y][x] === '>') {
const xx = (x + 1) % grid[0].length;
if (grid[y][xx] === '.') {
nextGrid[y][xx] = '>';
movers++;
} else {
nextGrid[y][x] = '>';
}
} else if (grid[y][x] === 'v') {
nextGrid[y][x] = 'v';
}
}
}
grid = copy(nextGrid);
// console.log(movers);
// console.log(grid.join('\n'));
nextGrid = freshGrid(grid);
// check downs
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[0].length; x++) {
if (grid[y][x] === 'v') {
const yy = (y + 1) % grid.length;
if (grid[yy][x] === '.') {
nextGrid[yy][x] = 'v';
movers++;
} else {
nextGrid[y][x] = 'v';
}
} else if (grid[y][x] === '>') {
nextGrid[y][x] = '>';
}
}
}
grid = copy(nextGrid);
i++;
// console.log({ i, movers });
// console.log(grid.join('\n'));
if (movers === 0) {
console.log({ i });
break;
}
}
}
function copy(grid) {
return grid.map((row) => row.map((d) => d));
}
function freshGrid(grid) {
const g = [];
for (let y = 0; y < grid.length; y++) {
g[y] = [];
for (let x = 0; x < grid[0].length; x++) {
g[y][x] = '.';
}
}
return g;
}
init();