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LLLLLLLLLLLLLLLLL.LLLLLL.L.LLLLLL.LLLLLLLLLLL.LLLL..LLLLLL.LLLLLLL",
"LLLLLLLLLL.LLLL.LLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLL",
"LL..LL.....LLL......LL.........L...L..L......L.....LL........LLLL.....L..................LL....L.",
"LLLLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLL.L.LLLLLLLLLLLLL.LLLLL.LLLLLLLLLL.LLLLLLL.LLLLLLL",
"LLLLLLLLLLLLLLLLLLL.LLL.LLL.L.LLLLLLLLLLLLLLLLLL.LL.L.L.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL",
"LL.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLL.LLL.LL.LLLLLL.",
"LLLLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLLL",
".LLLLLLLLLLLL.L.LLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL",
"...L..L...L.....L....LL..L.....L.L..L...LLLL..L....LL.L....L.L.L......LL...L...L..LLLL.L..L......",
"LLLLLLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLL.LLLLLLLLLLL.LLLLLL...LLL.LLLLL.LLL.LLL.LLLLL..LLLLLLL",
"LLLLLLLLLL.LLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLL.LLLLLLL",
"LLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLL.L.L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LLLLL.LLLLLLL",
"LLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL",
"LL.LLLLLLLLLLLL.LLLLLLL.LLLL..LLLLLLL..LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLL",
"LLLLLLLL.L.LLLLLLLLLLLL.LLLLLLLLLLLLL..LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLLL",
"LLLLLLLL.LLLLLLLLLLLLLL.LLLLL.LLLLLLLLLL.LLLLLLL.LLLLLL.LLLLLL.L.LLLLLLLLL.LLLLLLL.LL.LLL.LLLLLLL",
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL",
"LLLLLLLLLLLLLLL.LLLLLLL.LLLLL.LLLLL.LLLLL.LLLLLL.L.LLLL.LLLLLLLL.LLLLLLLLLLLLLLLLL.LLL.LL.LLLLLL."];
let countAdj = (state, x, y) => {
let count = 0;
if (x > 0 && state[x - 1][y] == '#') count++;
if (x > 0 && y > 0 && state[x - 1][y - 1] == '#') count++;
if (y > 0 && state[x][y - 1] == '#') count++;
if (x < state.length - 1 && y > 0 && state[x + 1][y - 1] == '#') count++;
if (x < state.length - 1 && state[x + 1][y] == '#') count++;
if (x < state.length - 1 && y < state[0].length - 1 && state[x + 1][y + 1] == '#') count++;
if (y < state[0].length - 1 && state[x][y + 1] == '#') count++;
if (x > 0 && y < state[0].length - 1 && state[x - 1][y + 1] == '#') count++;
return count;
}
let step = (state, countFn, threshold) => {
let next = [];
for (let x = 0; x < state.length; ++x) {
let row = [];
for (let y = 0; y < state[x].length; ++y) {
switch (state[x][y]) {
case '.': row.push('.'); break;
case 'L': if (countFn(state, x, y) == 0) { row.push('#'); } else { row.push('L'); } break;
case '#': if (countFn(state, x, y) >= threshold) { row.push('L'); } else { row.push('#'); } break;
}
}
next.push(row);
}
return next;
};
let equal = (a, b) => {
for (let x = 0; x < a.length; ++x) {
for (let y = 0; y < a[x].length; ++y) {
if (a[x][y] != b[x][y]) return false;
}
}
return true;
}
let countTotalOccupied = (state) => {
let totalOccupied = 0;
for (let x = 0; x < state.length; ++x) {
for (let y = 0; y < state[x].length; ++y) {
if (state[x][y] == '#') totalOccupied++;
}
}
return totalOccupied;
};
let countOccupiedWithFn = (initialState, countFn, threshold) => {
let state = step(initialState, countFn, threshold);
let nextState = step(state, countFn, threshold);
while (!equal(state, nextState)) {
state = nextState;
nextState = step(state, countFn, threshold);
}
return countTotalOccupied(state);
}
console.log(countOccupiedWithFn(initialState, countAdj, 4));
let isOccupiedInDirection = (state, x, y, dx, dy) => {
do {
x += dx;
y += dy;
} while (x >= 0 && y >= 0 && x <= state.length - 1 && y <= state[0].length - 1 && state[x][y] == '.');
return x >= 0 && y >= 0 && x <= state.length - 1 && y <= state[0].length - 1 && state[x][y] == '#';
}
let countFirstVisible = (state, x, y) => {
let count = 0;
if (isOccupiedInDirection(state, x, y, 1, 0)) count++;
if (isOccupiedInDirection(state, x, y, 1, 1)) count++;
if (isOccupiedInDirection(state, x, y, 0, 1)) count++;
if (isOccupiedInDirection(state, x, y, -1, 1)) count++;
if (isOccupiedInDirection(state, x, y, -1, 0)) count++;
if (isOccupiedInDirection(state, x, y, -1, -1)) count++;
if (isOccupiedInDirection(state, x, y, 0, -1)) count++;
if (isOccupiedInDirection(state, x, y, 1, -1)) count++;
return count;
}
console.log(countOccupiedWithFn(initialState, countFirstVisible, 5));