|
@@ -1,4 +1,6 @@
|
|
-let sample_list = [
|
|
|
|
|
|
+open Base
|
|
|
|
+
|
|
|
|
+let sample_list : int list = [
|
|
1721;
|
|
1721;
|
|
979;
|
|
979;
|
|
366;
|
|
366;
|
|
@@ -7,42 +9,37 @@ let sample_list = [
|
|
1456;
|
|
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
|
|
|
|
-
|
|
|
|
|
|
+(* 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
|
|
|
|
|
|
-let (>>=) list fn = let _ = List.map fn list in None
|
|
|
|
|
|
+(* 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 part_one source =
|
|
let res = Base.With_return.with_return (fun r ->
|
|
let res = Base.With_return.with_return (fun r ->
|
|
source >>= fun a ->
|
|
source >>= fun a ->
|
|
source >>= fun b ->
|
|
source >>= fun b ->
|
|
- if a != b && a + b == 2020 then r.return (Some (a, b))
|
|
|
|
|
|
+ if a <> b && a + b = 2020 then r.return (Some (a, b))
|
|
) in
|
|
) in
|
|
match res with
|
|
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)
|
|
|
|
|
|
+ | 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 part_two source =
|
|
let res = Base.With_return.with_return (fun r ->
|
|
let res = Base.With_return.with_return (fun r ->
|
|
source >>= fun a ->
|
|
source >>= fun a ->
|
|
source >>= fun b ->
|
|
source >>= fun b ->
|
|
source >>= fun c ->
|
|
source >>= fun c ->
|
|
- if a != b && b != c && a != c && a + b + c == 2020 then r.return (Some (a, b, c))
|
|
|
|
|
|
+ if a <> b && b <> c && a <> c && a + b + c = 2020 then r.return (Some (a, b, c))
|
|
) in
|
|
) in
|
|
match res with
|
|
match res with
|
|
- | None -> Printf.printf "Unable to find result\n"
|
|
|
|
|
|
+ | None -> Stdio.printf "Unable to find result\n"
|
|
| Some (x, y, z) ->
|
|
| Some (x, y, z) ->
|
|
- Printf.printf "Got (%d * %d * %d) = %d\n" x y z (x * y * z)
|
|
|
|
|
|
+ Stdio.printf "Got (%d * %d * %d) = %d\n" x y z (x * y * z)
|
|
|
|
|
|
let main() =
|
|
let main() =
|
|
let source = load_list "input" in
|
|
let source = load_list "input" in
|