-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.ts
59 lines (40 loc) · 1.4 KB
/
day10.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
56
57
58
59
import { readlines } from "./io"
import { range } from "./util"
type Adapter = number
export const count = (adapters: readonly Adapter[], index: number): number => {
const seen: Record<number, number> = {}
const _count = (adapters: readonly Adapter[], index: number): number => {
if (index in seen) return seen[index]
if (index === adapters.length - 1) return 1
let branches = 0
const current = adapters[index]
for (const jIndex of range(index + 1, adapters.length)) {
const candidateNext = adapters[jIndex]
const canBranch = candidateNext - current <= 3
// console.log(`Observing ${current} - ${j}: canBranch ${canBranch}`)
if (canBranch) branches += _count(adapters, jIndex)
}
seen[index] = branches
return branches
}
return _count(adapters, index)
}
(async () => {
const raw = await readlines('day10.txt')
const adapters = raw.map((v) => parseInt(v)).sort((a, b) => a-b)
const wall = 0
const device = Math.max(...adapters) + 3
const total = [...adapters, device]
// console.log(total)
let diffs: number[] = []
total.reduce((p, c) => {
diffs.push(c - p)
return c
}, wall)
const ones = diffs.filter(v => v === 1).length
const threes = diffs.filter(v => v === 3).length
console.log(`${ones * threes}`)
const realTotal = [wall, ...adapters, device]
console.log(realTotal)
console.log(count(realTotal, 0))
})()