20 lines
831 B
JavaScript
20 lines
831 B
JavaScript
const fs = require('fs');
|
|
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
|
|
const arr = file.split('\n');
|
|
|
|
let highestSeatID = 0;
|
|
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;
|
|
});
|
|
|
|
console.log(`Total seats: ${arr.length}`);
|
|
console.log(`Highest seat ID: ${highestSeatID}`); |