-
-
Save antosan/c810d234e2f6ebe92dfcd104dd8082a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const util = require('util'); | |
| const readFile = util.promisify(fs.readFile); | |
| const getRowColumnSeatId = (boardingPassSeatNo) => { | |
| const rowData = boardingPassSeatNo.substring(0, 7).split(''); | |
| const columnData = boardingPassSeatNo.substring(7).split(''); | |
| const row = rowData.reduce( | |
| ({ start, end }, cur) => { | |
| const middle = Math.floor((start + end) / 2); | |
| return cur === 'F' ? { start, end: middle } : { start: middle + 1, end }; | |
| }, | |
| { start: 0, end: 127 }, | |
| ); | |
| const column = columnData.reduce( | |
| ({ start, end }, cur) => { | |
| const middle = Math.floor((start + end) / 2); | |
| return cur === 'L' ? { start, end: middle } : { start: middle + 1, end }; | |
| }, | |
| { start: 0, end: 7 }, | |
| ); | |
| return { row: row.start, column: column.start, seatId: row.start * 8 + column.start }; | |
| }; | |
| const main = async () => { | |
| const input = await readFile('./puzzle-input.txt', 'utf-8'); | |
| const puzzleInput = input.trim().split('\n'); | |
| const seatIds = puzzleInput.map((input) => getRowColumnSeatId(input).seatId); | |
| const sortedSeatIds = [...seatIds].sort((a, b) => a - b); | |
| const lowestSeatId = sortedSeatIds[0]; | |
| const highestSeatId = sortedSeatIds[sortedSeatIds.length - 1]; | |
| const allSeats = Array(highestSeatId - lowestSeatId + 1).fill().map((_, index) => lowestSeatId + index); | |
| const missingSeats = allSeats.filter((s) => !seatIds.includes(s)); | |
| console.log('Hightest Seat ID:', highestSeatId); | |
| console.log('Missing Seat IDs:', missingSeats); | |
| }; | |
| main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment