12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- open Base
- let sample_list : int list = [
- 1721;
- 979;
- 366;
- 299;
- 675;
- 1456;
- ]
- (* load the provided filename *)
- let load_list (filename : string) : int list =
- 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 part_one source =
- let res = Base.With_return.with_return (fun r ->
- source >>= fun a ->
- source >>= fun b ->
- if a <> b && a + b = 2020 then r.return (Some (a, b))
- ) in
- match res with
- | None -> Stdio.printf "Unable to find result\n"
- | Some (x, y) -> Stdio.printf "Got (%d * %d) = %d\n" x y (x * y)
- let part_two source =
- let res = Base.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))
- ) in
- match res with
- | None -> Stdio.printf "Unable to find result\n"
- | Some (x, y, z) ->
- Stdio.printf "Got (%d * %d * %d) = %d\n" x y z (x * y * z)
- let main() =
- let source = load_list "input" in
- part_one source;
- part_two source
- let () = main()
|