Browse Source

factor apart a bit

Getty Ritter 3 years ago
parent
commit
d98039d83c
1 changed files with 15 additions and 25 deletions
  1. 15 25
      01/main.ml

+ 15 - 25
01/main.ml

@@ -1,16 +1,7 @@
 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 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
@@ -19,31 +10,30 @@ let load_list (filename : string) : int list =
    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 =
-  let res = Base.With_return.with_return (fun r ->
+  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)
+      if a <> b && a + b = 2020 then r.return (Some [a; b])
+    )
 
 let part_two source =
-  let res = Base.With_return.with_return (fun r ->
+  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)
+      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
-  part_one source;
-  part_two source
+  print_result (part_one source);
+  print_result (part_two source)
 
 let () = main()