-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
87 lines (66 loc) · 2.07 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs')
function decodeBoardingPassRow(seatString) {
const N = 7;
const rowString = seatString.slice(0, N); // get the first N chars
let minRow = 0;
let maxRow = 2 ** N - 1;
for (let i = 0; i < rowString.length; i++) {
const c = String(rowString.charAt(i))
if (c == 'F') maxRow -= 2 ** (N - i - 1)
else if (c == 'B') minRow += 2 ** (N - i - 1)
}
if (minRow != maxRow) {
console.error('SOMETHING WEIRD HAPPENED');
}
return minRow;
}
function decodeBoardingPassColumn(seatString) {
const N = 3
const colString = seatString.slice(0 - N) // get the last N chars
let minCol = 0;
let maxCol = 2 ** N - 1;
for (let i = 0; i < colString.length; i++) {
const c = String(colString.charAt(i))
if (c == 'L') maxCol -= 2 ** (N - i - 1)
else if (c == 'R') minCol += 2 ** (N - i - 1)
}
if (minCol != maxCol) {
console.error('SOMETHING WEIRD HAPPENED');
}
return minCol;
}
function getSeatID(row, column) {
return row * 8 + column
}
const file = fs.readFileSync('input.txt').toString('utf8');
const boardingPasses = file.split('\n')
.filter(line => line.length == 10)
.map(seat => {
const row = decodeBoardingPassRow(seat)
const column = decodeBoardingPassColumn(seat)
return {
seat,
row,
column,
seatID: getSeatID(row, column),
}
})
// sort by seat ID, descending (for part 1)
.sort((a, b) => (a.seatID < b.seatID) ? 1 : (b.seatID < a.seatID ? -1 : 0))
// PART 1
console.log('Highest Seat ID:', boardingPasses[0])
// PART 2
const filledSeats = Array(128).fill().map(()=>Array(8).fill(false))
for (boardingPass of boardingPasses) {
filledSeats[boardingPass.row][boardingPass.column] = true
}
// tweak me
const numRowsToIgnoreOnFront = 12;
const numRowsToIgnoreOnBack = 5;
for (let row = numRowsToIgnoreOnFront; row < filledSeats.length - numRowsToIgnoreOnBack; row++) {
for (let column = 0; column < filledSeats[0].length; column++) {
if (!filledSeats[row][column]) {
console.log('missing', row, column, ' seat ID:', getSeatID(row, column))
}
}
}