Skip to content

Instantly share code, notes, and snippets.

@Friss
Created December 11, 2020 06:31
Show Gist options
  • Save Friss/11bc8ce1657af4ec7b49d8d1a74c6fc9 to your computer and use it in GitHub Desktop.
Save Friss/11bc8ce1657af4ec7b49d8d1a74c6fc9 to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
const partOne = (grid) => {
let numChanges = 1;
while (numChanges > 0) {
numChanges = 0;
const copyOfGrid = [];
for (let row = 0; row < grid.length; row++) {
const gridRow = grid[row];
copyOfGrid.push([...gridRow]);
}
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) {
const seat = grid[row][col];
if (seat === '.') {
continue;
}
let countEmpty = 0;
for (let nextRow = -1; nextRow < 2; nextRow++) {
for (let nextCol = -1; nextCol < 2; nextCol++) {
if (nextRow === 0 && nextCol === 0) continue;
if (row + nextRow < 0 || row + nextRow >= grid.length) {
countEmpty++;
continue;
}
if (col + nextCol < 0 || col + nextCol >= grid[0].length) {
countEmpty++;
continue;
}
if (grid[row + nextRow][col + nextCol] !== '#') {
countEmpty++;
}
}
}
if (countEmpty === 8 && seat === 'L') {
copyOfGrid[row][col] = '#';
numChanges++;
} else if (seat === '#' && countEmpty <= 4) {
copyOfGrid[row][col] = 'L';
numChanges++;
}
}
}
grid = copyOfGrid;
}
let countPeople = 0;
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid.length; col++) {
if (grid[row][col] === '#') countPeople++;
}
}
return countPeople;
};
const partTwo = (grid) => {
let numChanges = 1;
while (numChanges > 0) {
numChanges = 0;
const copyOfGrid = [];
for (let row = 0; row < grid.length; row++) {
const gridRow = grid[row];
copyOfGrid.push([...gridRow]);
}
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) {
const seat = grid[row][col];
if (seat === '.') {
continue;
}
let countEmpty = 0;
const movements = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
movements.forEach(([deltaX, deltaY]) => {
let nextRow = row;
let nextCol = col;
while (true) {
nextRow += deltaX;
nextCol += deltaY;
if (nextRow < 0 || nextRow >= grid.length) {
countEmpty++;
break;
}
if (nextCol < 0 || nextCol >= grid[0].length) {
countEmpty++;
break;
}
if (grid[nextRow][nextCol] === 'L') {
countEmpty++;
break;
}
if (grid[nextRow][nextCol] === '#') {
break;
}
}
});
if (countEmpty === 8 && seat === 'L') {
copyOfGrid[row][col] = '#';
numChanges++;
} else if (seat === '#' && countEmpty <= 3) {
copyOfGrid[row][col] = 'L';
numChanges++;
}
}
}
grid = copyOfGrid;
}
let countPeople = 0;
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid.length; col++) {
if (grid[row][col] === '#') countPeople++;
}
}
return countPeople;
};
(async () => {
console.log(`Reading input from ${__dirname}/${process.argv[2]}.txt`);
const inputData = await fs.readFile(`${__dirname}/${process.argv[2]}.txt`);
const inputs = inputData
.toString()
.split('\n')
.filter((n) => n);
let grid = [];
inputs.forEach((line) => {
const row = [];
line.split('').forEach((s) => {
row.push(s);
});
grid.push(row);
});
console.log('partOne', partOne(grid));
console.log('partTwo', partTwo(grid));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment