advent-of-code/2020/05/solution1.js

30 lines
628 B
JavaScript
Raw Normal View History

2020-12-05 09:16:23 +01:00
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));