2020-12-02 17:47:24 -05:00
|
|
|
type line = {first: int; second: int; char: char; pass: string}
|
2020-12-02 06:05:38 -05:00
|
|
|
|
2020-12-02 17:47:24 -05:00
|
|
|
let parse_line str = Scanf.sscanf str "%d - %d %c : %s"
|
|
|
|
(fun first second char pass -> {first; second; char; pass})
|
2020-12-02 06:05:38 -05:00
|
|
|
|
|
|
|
|
2020-12-02 17:47:24 -05:00
|
|
|
let count_str c str =
|
|
|
|
let res = ref 0 in
|
|
|
|
String.iter (fun d -> if c = d then incr res) str;
|
|
|
|
!res
|
2020-12-02 06:05:38 -05:00
|
|
|
|
2020-12-02 17:47:24 -05:00
|
|
|
let ok1 {first; second; char; pass} =
|
|
|
|
let res = count_str char pass in
|
|
|
|
res >= first && res <= second
|
2020-12-02 06:05:38 -05:00
|
|
|
|
2020-12-02 17:47:24 -05:00
|
|
|
let ok2 {first; second; char; pass} =
|
|
|
|
(pass.[first - 1] = char) <> (pass.[second - 1] = char)
|
2020-12-02 06:05:38 -05:00
|
|
|
|
|
|
|
|
2020-12-02 17:47:24 -05:00
|
|
|
let main' ok input =
|
|
|
|
let count_all = ref 0 in
|
|
|
|
let count_ok = ref 0 in
|
2020-12-02 09:10:29 -05:00
|
|
|
let check l =
|
|
|
|
incr count_all;
|
2020-12-02 17:47:24 -05:00
|
|
|
if ok l then incr count_ok in
|
|
|
|
Bracket.infile_iter_lines input ~line:parse_line ~iter:check;
|
|
|
|
Printf.printf "%d out of %d ok\n" !count_ok !count_all
|
2020-12-02 09:10:29 -05:00
|
|
|
|
2020-12-03 09:11:29 -05:00
|
|
|
let main = Misc.main 2 [|main' ok1; main' ok2|]
|
2020-12-02 09:10:29 -05:00
|
|
|
|
|
|
|
|
|
|
|
let%expect_test "part 1" =
|
2020-12-03 09:11:29 -05:00
|
|
|
main' ok1 "../../data/day2";
|
2020-12-02 09:10:29 -05:00
|
|
|
[%expect{| 607 out of 1000 ok |}]
|
|
|
|
|
|
|
|
let%expect_test "part 2" =
|
2020-12-03 09:11:29 -05:00
|
|
|
main' ok2 "../../data/day2";
|
2020-12-02 09:10:29 -05:00
|
|
|
[%expect{| 321 out of 1000 ok |}]
|