-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
43 lines (29 loc) · 1.05 KB
/
index.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
import { Examples, Solution } from '~types'
import { curry } from 'ramda'
export const parse = (input: string) => input.replace(/\r/g, '')
export function predict(str: string): number {
const history = str.split(' ').map(Number)
const seq = [history]
while (seq.at(-1).some((n) => n !== 0)) {
const last = seq.at(-1)
const next = last.slice(1).map((v, i) => v - last[i])
seq.push(next)
}
seq.at(-1).push(0)
for (let i = seq.length - 2; i >= 0; i--) {
seq[i].push(seq[i].at(-1) + seq[i + 1].at(-1))
}
return seq[0].at(-1)
}
const getExtrapolatedSum = curry((backwards = false, input: string) =>
input
.split('\n')
.map((line) => (backwards ? line.split(' ').reverse().join(' ') : line))
.map(predict)
.reduce((acc, n) => acc + n, 0),
)
export const p1: Solution<typeof parse> = getExtrapolatedSum
export const p2: Solution<typeof parse> = getExtrapolatedSum(true)
export const p1ex: Examples = [{ expected: 0, input: '' }]
export const p2ex: Examples = [{ expected: 0, input: '' }]
export const onlyEx = true