-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path25.js
72 lines (64 loc) · 1.29 KB
/
25.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
'use strict';
/**
*
* @param {string[][]} grid
* @returns {boolean} If any movement was done
*/
function doStep(grid) {
let moved = false;
const height = grid.length;
const width = grid[0].length;
const movement = [];
// East movement check
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (grid[y][x] == '>') {
const target = (x + 1) % width;
if (grid[y][target] == '.') {
movement.push([y, x, target]);
moved = true;
}
}
}
}
// East movement
while (movement.length) {
const [y, x, target] = movement.shift();
grid[y][x] = '.';
grid[y][target] = '>';
}
// South movement check
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (grid[y][x] == 'v') {
const target = (y + 1) % height;
if (grid[target][x] == '.') {
movement.push([y, x, target]);
moved = true;
}
}
}
}
// South movement
while (movement.length) {
const [y, x, target] = movement.shift();
grid[y][x] = '.';
grid[target][x] = 'v';
}
return moved;
}
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split('\n').map(e => e.split(''));
let steps = 1;
while (doStep(data)) {
steps++;
}
return steps;
};
/**
* Day 25 doesn't have a part 2
*/
export const part2 = async () => 0;