main.ml 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. open Base
  2. let sample_list : int list = [
  3. 1721;
  4. 979;
  5. 366;
  6. 299;
  7. 675;
  8. 1456;
  9. ]
  10. (* load the provided filename *)
  11. let load_list (filename : string) : int list =
  12. let open Stdio.In_channel in
  13. let lines = with_file ~f:input_lines filename
  14. in List.map ~f:Int.of_string lines
  15. (* You might notice that this isn't actually >>=: I just wanted to
  16. have a non-parenthesized foreach! *)
  17. let (>>=) list fn = let _ = List.map ~f:fn list in None
  18. let part_one source =
  19. let res = Base.With_return.with_return (fun r ->
  20. source >>= fun a ->
  21. source >>= fun b ->
  22. if a <> b && a + b = 2020 then r.return (Some (a, b))
  23. ) in
  24. match res with
  25. | None -> Stdio.printf "Unable to find result\n"
  26. | Some (x, y) -> Stdio.printf "Got (%d * %d) = %d\n" x y (x * y)
  27. let part_two source =
  28. let res = Base.With_return.with_return (fun r ->
  29. source >>= fun a ->
  30. source >>= fun b ->
  31. source >>= fun c ->
  32. if a <> b && b <> c && a <> c && a + b + c = 2020 then r.return (Some (a, b, c))
  33. ) in
  34. match res with
  35. | None -> Stdio.printf "Unable to find result\n"
  36. | Some (x, y, z) ->
  37. Stdio.printf "Got (%d * %d * %d) = %d\n" x y z (x * y * z)
  38. let main() =
  39. let source = load_list "input" in
  40. part_one source;
  41. part_two source
  42. let () = main()