main.ml 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. let sample_list = [
  2. 1721;
  3. 979;
  4. 366;
  5. 299;
  6. 675;
  7. 1456;
  8. ]
  9. let load_list filename =
  10. let lines = ref [] in
  11. let f = open_in filename in
  12. try
  13. while true
  14. do
  15. lines := int_of_string (input_line f) :: !lines
  16. done; !lines
  17. with End_of_file ->
  18. close_in f;
  19. List.rev !lines
  20. let (>>=) list fn = let _ = List.map fn list in None
  21. let part_one source =
  22. let res = Base.With_return.with_return (fun r ->
  23. source >>= fun a ->
  24. source >>= fun b ->
  25. if a != b && a + b == 2020 then r.return (Some (a, b))
  26. ) in
  27. match res with
  28. | None -> Printf.printf "Unable to find result\n"
  29. | Some (x, y) -> Printf.printf "Got (%d * %d) = %d\n" x y (x * y)
  30. let part_two source =
  31. let res = Base.With_return.with_return (fun r ->
  32. source >>= fun a ->
  33. source >>= fun b ->
  34. source >>= fun c ->
  35. if a != b && b != c && a != c && a + b + c == 2020 then r.return (Some (a, b, c))
  36. ) in
  37. match res with
  38. | None -> Printf.printf "Unable to find result\n"
  39. | Some (x, y, z) ->
  40. Printf.printf "Got (%d * %d * %d) = %d\n" x y z (x * y * z)
  41. let main() =
  42. let source = load_list "input" in
  43. part_one source;
  44. part_two source
  45. let () = main()