-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.ts
67 lines (58 loc) · 1.93 KB
/
day4.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
60
61
62
63
64
65
66
67
import type { Input, SampleInput } from "./day4.input";
import type {
AddTDNum,
AsLines,
FilterTuple,
Pow2TNum,
SumTDNums,
TDNum,
TDNum1,
TDNumToString,
TNum0,
ToTDNum,
} from "./utils";
type SolvePart1<T> = TDNumToString<SumTDNums<MapLinesPart1<AsLines<T>>>>;
type MapLinesPart1<T> = {
[I in keyof T]: T[I] extends `Card ${any}:${infer W} |${infer N}` ? ToTDNum<
FilterTuple<
ParseNumbers<W>[number],
ParseNumbers<N>
> extends [any, ...infer Rest] ? Pow2TNum<Rest>["length"]
: 0
>
: never;
};
type ParseNumbers<T, State extends any[] = []> = T extends ` ${infer D}${infer Rest}`
? ParseNumbers<Rest, [...State, D]>
: T extends ` ${infer D0}${infer D1}${infer Rest}` ? ParseNumbers<Rest, [...State, `${D0}${D1}`]>
: State;
export type SampleResultPart1 = SolvePart1<SampleInput>;
// export type ResultPart1 = SolvePart1<Input>;
type SolvePart2<T> = TDNumToString<RecursePart2<AsLines<T>>>;
type RecursePart2<Lines, State extends TDNum = TNum0, LineCounts = []> =
/* dprint-ignore */
Lines extends [infer Line, ...infer LinesRest]
? LineCounts extends [infer LineCount, ...infer LineCountsRest]
? RecursePart2<
LinesRest,
AddTDNum<State, LineCount>,
UpdateLineCounts<LineCount, LineScore<Line>, LineCountsRest>
>
: RecursePart2<
LinesRest,
AddTDNum<State, TDNum1>,
UpdateLineCounts<TDNum1, LineScore<Line>, []>
>
: State;
type LineScore<T> = T extends `Card ${any}:${infer W} |${infer N}`
? FilterTuple<ParseNumbers<W>[number], ParseNumbers<N>>
: never;
type UpdateLineCounts<N, S, T extends any[], State extends any[] = []> =
/* dprint-ignore */
S extends [any, ...infer SRest]
? T extends [infer X, ...infer TRest]
? UpdateLineCounts<N, SRest, TRest, [...State, AddTDNum<X, N>]>
: UpdateLineCounts<N, SRest, [], [...State, AddTDNum<TDNum1, N>]>
: [...State, ...T];
export type SampleResultPart2 = SolvePart2<SampleInput>;
// export type ResultPart2 = SolvePart2<Input>;