-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.ts
48 lines (33 loc) · 1.13 KB
/
day09.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
import { readlines } from "./io"
const getValidValues = (preamble: number[]): number[] =>
preamble.map(i =>
preamble.map(j => i !== j ? i + j : undefined)
)
.flat()
.filter((v) => v !== undefined);
const findInvalid = (code: number[]): number => {
const preambleIndex = 25
for (const [i, v] of code.entries()) {
if (i < preambleIndex) continue
const preamble = code.slice(i - preambleIndex, i)
const validValues = getValidValues(preamble)
if (!validValues.includes(v)) return v
}
}
const findContiguousSumRange = (code: number[], invalid: number): number[] => {
for (const [indexI, _vi] of code.entries()) {
for (const [indexJ, _vj] of code.entries()) {
if (indexI === indexJ) continue
const candidate = code.slice(indexI, indexJ)
if (candidate.reduce((p, v) => p + v, 0) === invalid) return candidate
}
}
}
(async () => {
const raw = await readlines('day09.txt')
const code = raw.map((v) => parseInt(v))
const invalid = findInvalid(code)
console.log(invalid)
const range = findContiguousSumRange(code, invalid)
console.log(Math.min(...range) + Math.max(...range))
})()