12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- let sample_list = [
- 1721;
- 979;
- 366;
- 299;
- 675;
- 1456;
- ]
- let load_list filename =
- let lines = ref [] in
- let f = open_in filename in
- try
- while true
- do
- lines := int_of_string (input_line f) :: !lines
- done; !lines
- with End_of_file ->
- close_in f;
- List.rev !lines
- let (>>=) list fn = let _ = List.map 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 -> Printf.printf "Unable to find result\n"
- | Some (x, y) -> Printf.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 -> Printf.printf "Unable to find result\n"
- | Some (x, y, z) ->
- Printf.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()
|