-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday25.js
64 lines (55 loc) · 1.6 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const fs = require('fs');
const cl = console.log;
const empty = '.';
fs.readFile('day25.txt', 'utf-8', (err, input) => {
if (err) throw err;
const lines = input.trim().split(/\r?\n/);
const seabed = lines.map(ln => [...ln]);
cl(seaCucumber(seabed));
cl('Merry Christmas!');
});
function seaCucumber(seabed) {
let movedEast, movedSouth, steps = 0;
do {
steps++;
movedEast = migrateEast(seabed, '>');
seabed = transpose(seabed);
movedSouth = migrateEast(seabed, 'v');
seabed = transpose(seabed);
} while (movedEast + movedSouth > 0);
return steps;
}
function transpose(seabed) {
return seabed[0].map((_, y) => seabed.map(row => row[y]));
}
function migrateEast(seabed, cucumber) {
const origSeabed = copy(seabed);
let moveCount = 0;
const nextEast = ([x, y]) => [(x + 1) % origSeabed[0].length, y];
const get = ([x, y]) => origSeabed[y][x];
const set = ([x, y], v) => seabed[y][x] = v;
for (let y = 0; y < seabed.length; y++) {
for (let x = 0; x < seabed[0].length; x++) {
const pos = [x, y];
if (get(pos) === cucumber) {
const next = nextEast(pos);
if (get(next) === empty) {
set(next, cucumber);
set(pos, empty);
x++;
moveCount++;
}
}
}
}
return moveCount;
}
function copy(seabed) {
return seabed.map(row => row.slice(0));
}
function display(seabed) {
for (const row of seabed) {
cl(row.join(''));
}
}