AdventOfCode2020/day_5/task_2.js
2020-12-05 13:17:36 +01:00

36 lines
1.2 KiB
JavaScript

const fs = require('fs');
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
const arr = file.split('\n');
let highestSeatID = 0;
let existingSeats = {};
arr.forEach(seatIdentifier => {
let rowInstructions = seatIdentifier.replace(/[L|R]/g, '').replace(/B/g, 1).replace(/F/g, 0).split('');
let colInstructions = seatIdentifier.replace(/[F|B]/g, '').replace(/R/g, 1).replace(/L/g, 0).split('');
let row = 128, col = 8, mR = row/2, mC = col/2;
rowInstructions.forEach(bit => { bit == 1 ? null : row = row - mR ; mR/=2 });
colInstructions.forEach(bit => { bit == 1 ? null : col = col - mC ; mC/=2 });
row--; col--;
let seatID = row * 8 + col;
if (seatID > highestSeatID) highestSeatID = seatID;
existingSeats[`${row}-${col}`] = seatID;
});
let lowR = 0, lowS = 0;
let found = false;
for (let i=0; i<=127; i++) {
for (let j=0; j<=7; j++) {
if (!existingSeats[`${i}-${j}`]) {
if (lowR+1 == i) lowR = i;
if (i > lowR && !found) {
found = true;
console.log(`Missing seat ${i}-${j} with ID ${i*8+j}`);
}
}
}
}
console.log(`Total seats: ${arr.length}`);