-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
42 lines (37 loc) · 1.11 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
import { type RustIterator, r } from '@ekwoka/rust-ts';
import type { AOCInput } from '../../utils';
/**
* --- Day 9: Mirage Maintenance ---
*/
export const partOne = (input: AOCInput): number => {
return input
.lines()
.map((line) => line.words().map(Number).collect())
.flatMap(mapHistorySequence)
.map((sequence) => sequence.at(-1))
.sum();
};
export const partTwo = (input: AOCInput): number => {
return input
.lines()
.map((line) => line.words().map(Number).reverse().collect())
.flatMap(mapHistorySequence)
.map((sequence) => sequence.at(-1))
.sum();
};
const reduceHistorySteps = (history: [number[]]): number[] => {
const step = history[0]
.toIter()
.window(2)
.map(([prev, curr]) => curr - prev)
.collect();
return (history[0] = step);
};
const mapHistorySequence = (sequence: number[]): RustIterator<number[]> => {
return [sequence]
.toIter()
.chain(r`1..`.scan(reduceHistorySteps, sequence).takeWhile(notAllZeroes));
};
const notAllZeroes = (sequence: number[]): boolean => {
return sequence.length && sequence.some((num) => num !== 0);
};