Skip to content

Commit 69aea80

Browse files
feat(2020-day-05): calculate seat details from seat code
seat code is basically a bitmap. converting to binary and casting to integers makes it a lot simpler to figure out the details without having to think about it
1 parent 070cbe2 commit 69aea80

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

2020/day-05/seats.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Seat codes are basically bitmaps, so convert them
2+
const seatCodeToBitmap = (seatCode) => {
3+
return seatCode
4+
.replace(/F/g, 0)
5+
.replace(/B/g, 1)
6+
.replace(/L/g, 0)
7+
.replace(/R/g, 1)
8+
}
9+
10+
const calcSeatId = ({ row, column }) => row * 8 + column
11+
12+
const getSeat = (seatCode) => {
13+
// Seat codes are basically bitmaps, so convert them
14+
const seatBitMap = seatCodeToBitmap(seatCode)
15+
16+
// First octect is row
17+
const row = parseInt(seatBitMap.slice(0, 7), 2)
18+
// Last triplet is column
19+
const column = parseInt(seatBitMap.slice(7), 2)
20+
21+
return {
22+
row,
23+
column,
24+
id: calcSeatId({ row, column })
25+
}
26+
}
27+
28+
module.exports = {
29+
getSeat
30+
}

2020/day-05/seats.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { getSeat } = require('./seats')
4+
5+
describe('--- Day 5: Binary Boarding ---', () => {
6+
describe('Part 1', () => {
7+
describe('getSeat()', () => {
8+
it('returns the seat details for a given seat code', () => {
9+
expect(getSeat('BFFFBBFRRR')).to.deep.equal({
10+
row: 70,
11+
column: 7,
12+
id: 567
13+
})
14+
expect(getSeat('FFFBBBFRRR')).to.deep.equal({
15+
row: 14,
16+
column: 7,
17+
id: 119
18+
})
19+
expect(getSeat('BBFFBBFRLL')).to.deep.equal({
20+
row: 102,
21+
column: 4,
22+
id: 820
23+
})
24+
})
25+
})
26+
})
27+
})

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