Advent of Code, Day 5: Binary Boarding

Go to Day 5 Challenge | Go to index
I ran this code in the devtoolbar in Chrome, and the answers were simply outputted to the console. Learn more about my solution.

Part I

"You write a quick program to use your phone's camera to scan all of the nearby boarding passes (your puzzle input); perhaps you can find your seat through process of elimination. Instead of zones or groups, this airline uses binary space partitioning to seat people. A seat might be specified like FBFBBFFRLR, where F means "front", B means "back", L means "left", and R means "right"." ref
var calcSeatId = (seat) => {
  var minRow = 0
  var maxRow = 127
  var minCol = 0
  var maxCol = 7;

  [...seat].forEach(s => {
      if(s === "F" || s === "B"){
          var diffRow = maxRow - minRow
          var halfRow = Math.ceil(diffRow/2)-1

          if(s === "F") {
              maxRow = minRow + halfRow
          }
          if(s === "B") {
              minRow = maxRow - halfRow
          }
      }
      if(s === "R" || s === "L"){
          var diffCol = maxCol - minCol
          var halfCol = Math.ceil(diffCol/2)-1

          if(s === "L") {
              maxCol = minCol+ halfCol
          }
          if(s === "R") {
              minCol = maxCol - halfCol
          }
      }
  })

  return minRow * 8 + minCol
}

var allSeatIds = []
seats.forEach(s => allSeatIds.push({ code: s, seatId: calcSeatId(s) }))

console.log('highest seat id', allSeatIds.sort((a,b) => a.seatId < b.seatId ? 1 : -1)[0])

Part II

"It's a completely full flight, so your seat should be the only missing boarding pass in your list. However, there's a catch: some of the seats at the very front and back of the plane don't exist on this aircraft, so they'll be missing from your list as well. Your seat wasn't at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list." ref
allSeatIds.sort((a,b) => a.seatId > b.seatId ? 1 : -1).forEach((seat, index) => {
    if(seat.seatId > 0) {
        if(seat.seatId + 1 !== allSeatIds[index+1].seatId){
            console.warn('missing seat',seat.seatId + 1)
        }
    }
})
 
Created with ❤️ by David Lozzi

Advent of Code
CSS provided by Skeleton
Code highlighting provided by highlight.js

See a problem? Want to share how you solved it? Something else? I want to hear it! Share here!