You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
700 B
JavaScript

4 years ago
const { readInput } = require("./util");
const { lines } = readInput();
function binToInt(binString, oneChar = "1") {
const binArr = Array.from(binString).reverse();
let num = 0;
for (let i = 0; i < binArr.length; i++) {
if (binArr[i] === oneChar) {
num += 2 ** i;
}
}
return num;
}
function seatId(pass) {
const row = binToInt(pass.slice(0, 7), "B");
const col = binToInt(pass.slice(7, 11), "R");
return row * 8 + col;
}
const seatIds = new Set(lines.map(seatId));
const maxSeatId = seatId("BBBBBBBRRR");
console.log(maxSeatId);
for (let i = 0; i <= maxSeatId; i++) {
if (!seatIds.has(i) && seatIds.has(i - 1) && seatIds.has(i + 1)) {
console.log(i);
}
}