-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.ts
55 lines (38 loc) · 1.39 KB
/
day05.ts
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
import { readlines } from "./io"
type Partition = 'upper' | 'lower'
type MinMax = { min: number, max: number }
const binaryPartition = ({ min, max }: MinMax, code: readonly Partition[]): number => {
if (code.length === 0) throw new Error('Requires non-empty partition array')
const partition = code[0]
const remainder = code.slice(1)
if (remainder.length === 0) {
return partition === 'lower' ? min : max
}
// +1 for zero index
const half = ((max - min + 1) / 2)
const newRange = partition === 'lower'
? { min, max: max - half }
: { min: min + half, max }
return binaryPartition(newRange, remainder)
}
const parsePass = (p: string) => {
const match = /^(?<row>[B|F]{7})(?<col>[R|L]{3})$/.exec(p)
const rowCode = match.groups.row.split('').map((v) => v === 'B' ? 'upper' : 'lower')
const colCode = match.groups.col.split('').map((v) => v === 'R' ? 'upper' : 'lower')
const row = binaryPartition({ min: 0, max: 127 }, rowCode)
const col = binaryPartition({ min: 0, max: 7 }, colCode)
return { row, col }
}
(async () => {
const raw = await readlines('day05.txt')
const parsed = raw.map(parsePass)
const seatIds = parsed.map((v) => v.row * 8 + v.col)
console.log(Math.max(...seatIds))
seatIds.sort((a, b) => a - b)
seatIds.reduce((p, c) => {
if (p + 1 !== c) {
console.log(`Your seat: ${p+1}`)
}
return c
})
})()