aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2024/day_01.ml32
-rw-r--r--2024/dune2
-rw-r--r--2024/dune-project1
3 files changed, 35 insertions, 0 deletions
diff --git a/2024/day_01.ml b/2024/day_01.ml
new file mode 100644
index 0000000..42ee50d
--- /dev/null
+++ b/2024/day_01.ml
@@ -0,0 +1,32 @@
+let filename = "./day_01_input.txt"
+
+let () =
+ let ic = open_in filename in
+
+ let try_read () = try Some (input_line ic) with End_of_file -> None in
+
+ let rec read_lines left right =
+ match try_read () with
+ | Some line -> (
+ let ints =
+ Str.split_delim (Str.regexp " ") line |> List.map int_of_string
+ in
+ match ints with
+ | [ a; b ] -> read_lines (a :: left) (b :: right)
+ | _ -> failwith "invalid")
+ | None ->
+ close_in ic;
+ (List.sort compare left, List.sort compare right)
+ in
+ let left, right = read_lines [] [] in
+ let sum =
+ List.map2 ( - ) left right |> List.map abs |> List.fold_left ( + ) 0
+ in
+ Printf.sprintf "Sum %d" sum |> print_endline;
+ let sim =
+ List.map
+ (fun x -> x * (List.filter (fun y -> x == y) right |> List.length))
+ left
+ |> List.fold_left ( + ) 0
+ in
+ Printf.sprintf "Sim %d" sim |> print_endline
diff --git a/2024/dune b/2024/dune
new file mode 100644
index 0000000..2a7fc2a
--- /dev/null
+++ b/2024/dune
@@ -0,0 +1,2 @@
+(executable (name day_01)
+(libraries str))
diff --git a/2024/dune-project b/2024/dune-project
new file mode 100644
index 0000000..0a32580
--- /dev/null
+++ b/2024/dune-project
@@ -0,0 +1 @@
+(lang dune 3.6)