-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (38 loc) · 1.04 KB
/
script.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
'use strict'
// Setup
const getSeatIDs = boardingPasses => {
return boardingPasses.split('\n')
.map(boardingPass => {
return parseInt(boardingPass.replace(/F|L/g, '0').replace(/B|R/g, '1'), 2)
})
}
// Part 1
// ======
const part1 = input => {
return getSeatIDs(input)
.sort((a, b) => b - a)
.shift()
}
// Part 2
// ======
const part2 = input => {
const seats = getSeatIDs(input)
.sort((a, b) => a - b)
const minSeat = seats[0]
const maxSeat = seats[seats.length - 1]
for (var seat = minSeat; seat < maxSeat; seat++) {
if (!seats.includes(seat)) return seat
}
}
const part3 = input => {
return parseInt(input).toString(2)
.padStart(10, '0')
.replace(/^((?:0|1){7})((?:0|1){3})$/, (match, row, col) => {
return row.replace(/1/g, 'B').replace(/0/g, 'F') + col.replace(/1/g, 'R').replace(/0/g, 'L')
})
}
module.exports = { part1, part2, part3 }
//12:00 AM - start
//12:18 AM - getRange method
//12:21 AM - part 1
//12:23 AM - part 2