Published
Edited
Dec 12, 2020
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function q1(input) {
const height = input.length;
const width = input[0].length;
let state = [];
let state2 = [];
for (const row of input) {
state.push(...row);
state2.push(...row);
}
let changedState = true;
const { max, min } = Math;
const FLOOR = 0;
const EMPTY = 1;
const OCCUPIED = 10;
const CROWDED = OCCUPIED * 4;

const maxTurns = 100;
let turns = 0;
let results = [];
do {
changedState = false;
for (let y = 0; y < height; y++) {
const jStart = max(y - 1, 0);
const jEnd = min(y + 2, height);
for (let x = 0; x < width; x++) {
let seat = state[x + y * width];
// skip floor tiles;
if (seat === FLOOR) continue;

// count adjacent occupied seats

let adjacent = -seat; // we don't want to include our current seat

const iStart = max(x - 1, 0);
const iEnd = min(x + 2, width);
for (let j = jStart; j < jEnd; j++) {
for (let i = iStart; i < iEnd; i++) {
adjacent += state[i + j * width];
}
}
if (adjacent >= CROWDED && seat === OCCUPIED) {
changedState = true;
state2[x + y * width] = EMPTY;
} else if (adjacent < OCCUPIED && seat === EMPTY) {
changedState = true;
state2[x + y * width] = OCCUPIED;
}
}
}
for (let i = 0; i < state.length; i++) {
state[i] = state2[i];
}
results.push(makeTiles(state, width));
} while (changedState && ++turns < maxTurns);

let totalOccupied = 0;
for (const tile of state) {
totalOccupied += tile === OCCUPIED;
}
return { results, totalOccupied, turns, changedState, state };
}
Insert cell
answer1 = q1(input)
Insert cell
Insert cell
Insert cell
Insert cell
function q2(input, renderSize = 9) {
const height = input.length;
const width = input[0].length;
let state = [];
let state2 = [];
for (const row of input) {
state.push(...row);
state2.push(...row);
}
let changedState = true;
const { max, min } = Math;
const FLOOR = 0;
const EMPTY = 1;
const OCCUPIED = 10;
const CROWDED = OCCUPIED * 5;

const maxTurns = 100;
let turns = 0;
let results = [];
const directions = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1], // skip [0,0] since that's not going anywhere
[1, -1],
[1, 0],
[1, 1]
];
const inside = (x, y) => {
return x >= 0 && x < width && y >= 0 && y < height;
};
do {
changedState = false;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let seat = state[x + y * width];
// skip floor tiles;
if (seat === FLOOR) continue;

// count adjacent occupied seats
let adjacent = 0;
for (const [dx, dy] of directions) {
let i = dx,
j = dy;
let neighbor = FLOOR;
while (inside(x + i, y + j)) {
neighbor = state[x + i + (y + j) * width];
if (neighbor !== FLOOR) break;
i += dx;
j += dy;
}
adjacent += neighbor;
}

if (adjacent >= CROWDED && seat === OCCUPIED) {
changedState = true;
state2[x + y * width] = EMPTY;
} else if (adjacent < OCCUPIED && seat === EMPTY) {
changedState = true;
state2[x + y * width] = OCCUPIED;
}
}
}
for (let i = 0; i < state.length; i++) {
state[i] = state2[i];
}
results.push(makeTiles(state, width, renderSize));
} while (changedState && ++turns < maxTurns);

let totalOccupied = 0;
for (const tile of state) {
totalOccupied += tile === OCCUPIED;
}
return { results, totalOccupied, turns, changedState, state };
}
Insert cell
testAnswer2 = q2(testInput, 20)
Insert cell
testAnswer2.results[6]
Insert cell
answer2 = q2(input)
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more