-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.ml
66 lines (53 loc) · 1.43 KB
/
day02.ml
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
open! Core
open! Import
let date = 2
module Chars_by_count = struct
type t = char list Int.Map.t
let create word =
String.to_list word
|> List.map ~f:(fun x -> x, 1)
|> Char.Map.of_alist_reduce ~f:(+)
|> Map.to_alist
|> List.map ~f:Tuple2.swap
|> Int.Map.of_alist_multi
let has_count t ~count =
Map.find t count
|> Option.is_some
end
module Part01 = struct
module Input = Converters.String_list
module Output = Int
let part = 1
let solve input =
let counts = List.map input ~f:Chars_by_count.create in
List.count counts ~f:(Chars_by_count.has_count ~count:2)
* List.count counts ~f:(Chars_by_count.has_count ~count:3)
end
module Part02 = struct
module Input = Converters.String_list
module Output = String
let part = 2
let is_correct (a, b) =
let a = String.to_list a in
let b = String.to_list b in
List.zip_exn a b
|> List.count ~f:(fun (c1, c2) -> not (Char.equal c1 c2))
|> Int.equal 1
let common_letters word_a word_b =
String.to_list word_a
|> List.filter_mapi ~f:(fun i c ->
match Char.equal c word_b.[i] with
| true -> Some c
| false -> None)
|> String.of_char_list
let solve input =
let word_a, word_b =
List.cartesian_product input input
|> List.find_exn ~f:is_correct
in
common_letters word_a word_b
end
let parts : (module Solution.Part.Basic) list =
[ (module Part01)
; (module Part02)
]