-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.js
57 lines (46 loc) · 1.2 KB
/
day5.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
const {
pipe,
filter,
sort,
map,
head,
split,
splitAt,
reduce,
max,
} = require('ramda')
const { readFile } = require('./utils')
const input = readFile('./5.input.txt').map((contents) => contents.split('\n'))
const sliceFront = (items) => items.slice(0, items.length / 2)
const sliceBack = (items) => items.slice(items.length / 2)
const findSeatPart = (frontKey = 'F') => (boardingPass) => {
const seatParts = [...Array(frontKey === 'F' ? 128 : 8).keys()]
return pipe(
split(''),
reduce(
(acc, frontOrBack) =>
frontOrBack === frontKey ? sliceFront(acc) : sliceBack(acc),
seatParts,
),
head,
)(boardingPass)
}
const findRow = findSeatPart('F')
const findColumn = findSeatPart('L')
const findSeatId = (boardingPass) => {
const [rowPass, columnPass] = splitAt(7)(boardingPass)
return findRow(rowPass) * 8 + findColumn(columnPass)
}
const getSeatIds = map(findSeatId)
const star1 = input.map(getSeatIds).map(reduce(max, -Infinity))
const star2 = input.map(getSeatIds).map((ids) => {
return pipe(
filter((x) => ids.indexOf(x - 1) === -1 && ids.indexOf(x - 2) !== -1),
map((x) => x - 1),
head,
)(ids)
})
module.exports = {
star1,
star2,
}