Published
Edited
Dec 7, 2020
Insert cell
Insert cell
split = (min, max, code) => {
const midpoint = Math.floor((max - min) / 2) + min;
if (code === 'F' || code === 'L') {
return [min, midpoint];
} else {
return [midpoint + 1, max];
}
}
Insert cell
split(0, 127, 'F')
Insert cell
split(0, 63, 'B')
Insert cell
split(32,63,'F')
Insert cell
split(32, 47, 'B')
Insert cell
split(40,47,'B')
Insert cell
split(44,47,'F')
Insert cell
split(44,45,'F')
Insert cell
parseRow = ( code ) => {
let min = 0, max = 127;
for ( let i = 0; i < code.length; i++ ) {
[min, max] = split(min, max, code[i]);
}
return min;
}
Insert cell
parseRow('FBFBBFF')
Insert cell
parseSeat = ( code ) => {
let min = 0, max = 7;
for ( let i = 0; i < code.length; i++ ) {
[min, max] = split(min, max, code[i]);
}
return min;
}
Insert cell
parseSeat('RLR')
Insert cell
parseCode = code => {
// return [code.substring(0, 7), code.substring(7)];
const row = parseRow(code.substring(0, 7));
const seat = parseSeat(code.substring(7));
return row * 8 + seat;
}
Insert cell
parseCode('BFFFBBFRRR')
Insert cell
parseCode('FFFBBBFRRR')
Insert cell
parseCode('BBFFBBFRLL')
Insert cell
Insert cell
Insert cell
seatIds = input.split('\n').map(parseCode)
Insert cell
maxSeatId = Math.max.apply(null, seatIds)
Insert cell
Insert cell
minSeatId = Math.min.apply(null, seatIds)
Insert cell
{ for ( let i = minSeatId + 1; i < maxSeatId; i++ ) {
if ( ! seatIds.includes( i ) && seatIds.includes( i + 1 ) && seatIds.includes( i - 1 ) ) return i;
} }
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more