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.
30 lines
628 B
JavaScript
30 lines
628 B
JavaScript
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;
|
|
}
|
|
|
|
for (const line of lines.slice(0, 10)) {
|
|
console.log(line, seatId(line));
|
|
}
|
|
|
|
const seatIds = lines.map(seatId);
|
|
console.log(seatIds);
|
|
|
|
console.log(Math.max(...seatIds));
|