-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday02.ml
More file actions
28 lines (24 loc) · 825 Bytes
/
day02.ml
File metadata and controls
28 lines (24 loc) · 825 Bytes
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
let file_lines_fold name comb init =
let ic = open_in name
in let try_read () =
try Some (input_line ic) with End_of_file -> None
in let rec loop acc =
match try_read () with Some s -> loop (comb acc s) | None -> close_in ic; acc
in loop init
let pred1 lo hi c pw =
let count = List.init (String.length pw) (String.get pw) |> List.find_all ((=) c) |> List.length in
lo <= count && count <= hi
let pred2 lo hi c pw =
(pw.[lo - 1] = c) <> (pw.[hi - 1] = c)
let day02 filename part =
let pred = if part = 1 then pred1 else pred2 in
let comb passed line =
if Scanf.sscanf line "%d-%d %c: %s" pred then
passed + 1
else
passed
in file_lines_fold filename comb 0
let () =
let file = Sys.argv.(1) in
Printf.printf "%d\n" (day02 file 1);
Printf.printf "%d\n" (day02 file 2);