-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo.re
More file actions
50 lines (39 loc) · 1.25 KB
/
Two.re
File metadata and controls
50 lines (39 loc) · 1.25 KB
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
module CharMap = Map.Make(Char);
type boxId = string;
let inputLines = () =>
Node.Fs.readFileAsUtf8Sync("resources/two.txt")
|> Js.String.split("\n")
|> Array.to_list;
let arrayFromString = (s: string): array(char) =>
Array.init(String.length(s), String.get(s));
let characterHistogram = (arrayOfChars: array(char)) =>
Array.fold_left(
(map, aChar) =>
switch (CharMap.find(aChar, map)) {
| item => CharMap.add(aChar, item + 1, map)
| exception Not_found => CharMap.add(aChar, 1, map)
},
CharMap.empty,
arrayOfChars,
);
let hasValue = (value: 'a, map: CharMap.t('a)): bool =>
CharMap.exists((_, binding) => binding == value, map);
let boxIdHistogram = (boxId: boxId) =>
boxId |> arrayFromString |> characterHistogram;
let checksum = (boxIds: array(boxId)): int => {
let histograms = Array.map(boxIdHistogram, boxIds);
let numberOfTwos =
Array.fold_left(
(total, histogram) => hasValue(2, histogram) ? total + 1 : total,
0,
histograms,
);
let numberOfThrees =
Array.fold_left(
(total, histogram) => hasValue(3, histogram) ? total + 1 : total,
0,
histograms,
);
numberOfThrees * numberOfTwos;
};
let firstSolution = () => inputLines() |> Array.of_list |> checksum;