-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
113 lines (106 loc) · 2.27 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import run from "aocrunner";
import {
string as S,
readonlyArray,
array as A,
function as F,
option as O,
nonEmptyArray as NEA,
semigroup,
set,
} from "fp-ts";
const parseInput = (rawInput: string) =>
F.pipe(rawInput, S.trim, S.split(""), readonlyArray.toArray);
const toWindows =
(windowSize: number) =>
<T>(arr: T[]): T[][] =>
F.pipe(
NEA.range(0, windowSize - 1),
NEA.foldMap({
concat: (a, b) => F.pipe(A.zip(a, b), A.map(A.flatten)),
} as semigroup.Semigroup<T[][]>)((skip) =>
F.pipe(arr, A.map(A.of), A.dropLeft(skip)),
),
);
const toIndexAfterDistinctItems =
(distinctItemCount: number) => (array: string[]) =>
F.pipe(
array,
toWindows(distinctItemCount),
A.findIndex(
F.flow(
set.fromArray(S.Eq),
set.size,
(uniqueElems) => uniqueElems === distinctItemCount,
),
),
O.map((windowIndex) => windowIndex + distinctItemCount),
);
const part1 = (rawInput: string) =>
F.pipe(
rawInput,
parseInput,
toIndexAfterDistinctItems(4),
O.getOrElse(() => 0),
);
const part2 = (rawInput: string) =>
F.pipe(
rawInput,
parseInput,
toIndexAfterDistinctItems(14),
O.getOrElse(() => 0),
);
run({
part1: {
tests: [
{
input: `mjqjpqmgbljsphdztnvjfqwrcgsmlb`,
expected: 7,
},
{
input: `bvwbjplbgvbhsrlpgdmjqwftvncz`,
expected: 5,
},
{
input: `nppdvjthqldpwncqszvftbrmjlhg`,
expected: 6,
},
{
input: `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`,
expected: 10,
},
{
input: `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`,
expected: 11,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `mjqjpqmgbljsphdztnvjfqwrcgsmlb`,
expected: 19,
},
{
input: `bvwbjplbgvbhsrlpgdmjqwftvncz`,
expected: 23,
},
{
input: `nppdvjthqldpwncqszvftbrmjlhg`,
expected: 23,
},
{
input: `nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg`,
expected: 29,
},
{
input: `zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw`,
expected: 26,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});