123456789101112131415161718192021222324252627282930313233343536373839 |
- open Base
- (* load the provided filename *)
- let load_list filename =
- let open Stdio.In_channel in
- let lines = with_file ~f:input_lines filename
- in List.map ~f:Int.of_string lines
- (* You might notice that this isn't actually >>=: I just wanted to
- have a non-parenthesized foreach! *)
- let (>>=) list fn = let _ = List.map ~f:fn list in None
- let print_result = function
- | None -> Stdio.printf "Unable to find result\n"
- | Some xs ->
- let lst = String.concat ~sep:" * " (List.map ~f:Int.to_string xs)
- in Stdio.printf "Got (%s) = %d\n" lst (List.fold ~init:1 ~f:(fun x y -> x * y) xs)
- let part_one source =
- With_return.with_return (fun r ->
- source >>= fun a ->
- source >>= fun b ->
- if a <> b && a + b = 2020 then r.return (Some [a; b])
- )
- let part_two source =
- With_return.with_return (fun r ->
- source >>= fun a ->
- source >>= fun b ->
- source >>= fun c ->
- if a <> b && b <> c && a <> c && a + b + c = 2020 then r.return (Some [a; b; c])
- )
- let main() =
- let source = load_list "input" in
- print_result (part_one source);
- print_result (part_two source)
- let () = main()
|