-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem11a.js
58 lines (54 loc) · 1.31 KB
/
problem11a.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
const fs = require('fs');
let room = fs.readFileSync('input11.txt', 'utf8').split('\n').map(line => line.trim()).filter(line => line.length).map(s => s.split(''));
let rounds = 0;
let occupied;
while (true) {
let changed = false;
occupied = 0;
const next = [];
for (let y = 0; (y < room.length); y++) {
const row = [];
for (let x = 0; (x < room[y].length); x++) {
let neighbors = 0;
for (yi = y - 1; (yi <= y + 1); yi++) {
for (xi = x - 1; (xi <= x + 1); xi++) {
if ((yi < 0) || (yi >= room.length) || (xi < 0) || (xi >= room[0].length)) {
continue;
}
if ((xi === x) && (yi === y)) {
continue;
}
if (room[yi][xi] === '#') {
neighbors++;
}
}
}
if (room[y][x] === '#') {
if (neighbors >= 4) {
row.push('L');
changed = true;
} else {
row.push('#');
occupied++;
}
} else if (room[y][x] === 'L') {
if (!neighbors) {
row.push('#');
occupied++;
changed = true;
} else {
row.push('L');
}
} else {
row.push('.');
}
}
next.push(row);
}
room = next;
rounds++;
if (!changed) {
break;
}
}
console.log(rounds, occupied);