aoc2020/day2.ml

43 lines
1.1 KiB
OCaml
Raw Normal View History

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-02 17:47:24 -05:00
let mains = [|main' ok1; main' ok2|]
2020-12-02 06:05:38 -05:00
let main = function
| [part; input] -> mains.(int_of_string part - 1) input
2020-12-03 03:53:44 -05:00
| _ -> Usage.exit_default 2
2020-12-02 09:10:29 -05:00
let%expect_test "part 1" =
mains.(0) "../../data/day2";
[%expect{| 607 out of 1000 ok |}]
let%expect_test "part 2" =
mains.(1) "../../data/day2";
[%expect{| 321 out of 1000 ok |}]