-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
82 lines (74 loc) · 1.63 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
import run from "aocrunner";
import {
string as S,
readonlyArray,
array as A,
function as F,
string,
monoid,
number as N,
nonEmptyArray as NEA,
} from "fp-ts";
const parseInput = (rawInput: string) =>
F.pipe(
rawInput,
S.trim,
S.split("\n"),
readonlyArray.toArray,
A.map(F.flow(S.trim, S.split(""), readonlyArray.toArray)),
);
let strToScore = (str: string): number => (str.charCodeAt(0) - 38) % 58;
const monoidIntersection: monoid.Monoid<string[]> = {
concat: (a, b) => A.intersection(string.Eq)(a, b),
empty: [],
};
const toIntersectionScoreTotal: (
intersections: NEA.NonEmptyArray<string[]>[],
) => number = F.flow(
A.map(NEA.concatAll(monoidIntersection)),
A.map(A.lookup(0)),
A.compact,
A.map(strToScore),
monoid.concatAll(N.MonoidSum),
);
const part1 = (rawInput: string) =>
F.pipe(
rawInput,
parseInput,
A.map((str) => A.splitAt(str.length / 2)(str)),
toIntersectionScoreTotal,
);
const part2 = (rawInput: string) =>
F.pipe(rawInput, parseInput, A.chunksOf(3), toIntersectionScoreTotal);
run({
part1: {
tests: [
{
input: `vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`,
expected: 157,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`,
expected: 70,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});