Published
Edited
Dec 5, 2020
Insert cell
Insert cell
solve(input) // part 1
Insert cell
solve = data => data
.split("\n")
.map(seatID) // all seatIDs
.reduce((acc, inc) => inc > acc ? inc : acc, 0) // find largest
Insert cell
{
const ids = input
.split("\n")
.map(seatID)
.sort()
let missing = []
for(let i = 1; i < ids.length-2; i++) {
if(ids[i]-ids[i-1] > 1) missing.push({seatid: ids[i], index: i})
}
return missing
}
Insert cell
seatID = str => row(str) * 8 + col(str)
Insert cell
row = str =>
first7(str)
.split("")
.reduce((acc, inc) => inc == "F" ?
[acc[0], Math.floor((acc[1]-acc[0])/2) + acc[0]] : // Front (lower end)
[Math.ceil((acc[1]-acc[0])/2) + acc[0], acc[1]], // Back (upper end)
[0,127])[0]
Insert cell
first7 = str => str.substring(0,7)
Insert cell
col = str => last3(str)
.split("")
.reduce((acc, inc) => inc == "L" ?
[acc[0], Math.floor((acc[1]-acc[0])/2) + acc[0]] : // Front (lower end)
[Math.ceil((acc[1]-acc[0])/2) + acc[0], acc[1]], // Back (upper end)
[0,7])[0]
Insert cell
last3 = str => str.substring(7)
Insert cell
Insert cell
otherexampleinputs = ["BFFFBBFRRR","FFFBBBFRRR","BBFFBBFRLL"]
Insert cell
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