47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
|
const { Glib } = require('../../lib');
|
||
|
|
||
|
const START = 0b0;
|
||
|
const END = 0b1111111111;
|
||
|
|
||
|
const decode = (bp) => {
|
||
|
if (!bp.match(/^[bf]+[lr]+$/i, '')) {
|
||
|
throw new Error('invalid input');
|
||
|
}
|
||
|
const binary = bp.replace(/[br]/gi, '1').replace(/[fl]/gi, '0');
|
||
|
return BigInt('0b' + binary);
|
||
|
};
|
||
|
|
||
|
const encode = (seatId, columnCharacters, length) => {
|
||
|
const binary = seatId.toString(2).padStart(length, '0');
|
||
|
return (
|
||
|
binary
|
||
|
.slice(0, -columnCharacters)
|
||
|
.replace(/1/g, 'B')
|
||
|
.replace(/0/g, 'F') +
|
||
|
binary
|
||
|
.slice(-columnCharacters)
|
||
|
.replace(/1/g, 'R')
|
||
|
.replace(/0/g, 'L')
|
||
|
);
|
||
|
};
|
||
|
|
||
|
module.exports = {
|
||
|
'1': (input) =>
|
||
|
Glib.fromLines(input)
|
||
|
.map(decode)
|
||
|
.reduce((max, current) => (max > current ? max : current), -Infinity),
|
||
|
'2': (input) => {
|
||
|
const foundSid = new Map(
|
||
|
Glib.fromLines(input).map((bp) => [decode(bp), bp]),
|
||
|
);
|
||
|
return Glib.fromSequence(START, END)
|
||
|
.filter(
|
||
|
(sid) =>
|
||
|
!foundSid.has(sid) &&
|
||
|
foundSid.has(sid - 1n) &&
|
||
|
foundSid.has(sid + 1n),
|
||
|
)
|
||
|
.join();
|
||
|
},
|
||
|
};
|