Skip to content

Commit a397020

Browse files
feat(2020-day-05): find the first available seat
Sort the tickets, add a bunch of fake ones for the non-existent rows at the front of the plane, then find the gap where IDs stop matching the array position
1 parent 73b007f commit a397020

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

2020/day-05/seats.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,54 @@ const seatCodeToBitmap = (seatCode) => {
77
.replace(/R/g, 1)
88
}
99

10-
const calcSeatId = ({ row, column }) => row * 8 + column
10+
// This is just binary to int
11+
// const calcSeatId = ({ row, column }) => row * 8 + column
1112

1213
const getSeat = (seatCode) => {
1314
// Seat codes are basically bitmaps, so convert them
1415
const seatBitMap = seatCodeToBitmap(seatCode)
1516

16-
// First octect is row
17+
// First septuplet is row
1718
const row = parseInt(seatBitMap.slice(0, 7), 2)
1819
// Last triplet is column
1920
const column = parseInt(seatBitMap.slice(7), 2)
2021

2122
return {
2223
row,
2324
column,
24-
id: calcSeatId({ row, column })
25+
id: parseInt(seatBitMap, 2)
2526
}
2627
}
2728

29+
const findAvailableSeat = (seats) => {
30+
// We could do some complicated logic to loop through a sorted
31+
// list of seats comparing indexes to find the first one where there's
32+
// a gap by looking for an off-by-one situation while excluding first
33+
// and last rows:
34+
//
35+
// if (seat.row === 0 || seat.row === 127) return false
36+
// // Find gap between this seat and previous
37+
// return (
38+
// (seat.id - 1) !== arr[idx - 1].id
39+
// )
40+
//
41+
// Or we can just and a fake first row, and then look for any
42+
// seat where the ID doesn't match the array index
43+
//
44+
const occupied = seats.map(getSeat)
45+
.sort((a, b) => a.id - b.id)
46+
// The first seat is not in row 0, so add a bunch of
47+
// fake rows at the front of the plane
48+
occupied.unshift(...Array(occupied[0].id))
49+
// Find the gap, ignoring the fake seats
50+
return occupied.find((seat, idx, arr) => {
51+
// !!seat filters out the fake ones since they're undefined
52+
return !!seat && seat.id !== idx
53+
// we can ignore rows at the back even if missing
54+
}).id - 1 // The open seat will be immediately before the mismatched seat
55+
}
56+
2857
module.exports = {
29-
getSeat
58+
getSeat,
59+
findAvailableSeat
3060
}

2020/day-05/seats.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { getSeat } = require('./seats')
3+
const { getSeat, findAvailableSeat } = require('./seats')
44

55
describe('--- Day 5: Binary Boarding ---', () => {
66
describe('Part 1', () => {
@@ -23,5 +23,20 @@ describe('--- Day 5: Binary Boarding ---', () => {
2323
})
2424
})
2525
})
26+
describe('findAvailableSeat()', () => {
27+
it('finds the first available seat', () => {
28+
const secondRow = [
29+
'FFFFFFBLLL',
30+
'FFFFFFBLLR',
31+
'FFFFFFBLRL',
32+
'FFFFFFBLRR',
33+
// 'FFFFFFBRLL', row:1, col:4, id: 12
34+
'FFFFFFBRLR',
35+
'FFFFFFBRRL',
36+
'FFFFFFBRRR'
37+
]
38+
expect(findAvailableSeat(secondRow)).to.equal(12)
39+
})
40+
})
2641
})
2742
})

2020/day-05/solution.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { inputToArray } = require('../../2018/inputParser')
5-
const { getSeat } = require('./seats')
5+
const { getSeat, findAvailableSeat } = require('./seats')
66

77
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
88
if (err) throw err
@@ -24,9 +24,8 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
2424
}
2525

2626
const part2 = () => {
27-
const data = resetInput()
28-
console.debug(data)
29-
return 'No answer yet'
27+
const tickets = resetInput()
28+
return findAvailableSeat(tickets)
3029
}
3130
const answers = []
3231
answers.push(part1())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy