From 04b11a994f3ef63a150c047be6a94cd384804422 Mon Sep 17 00:00:00 2001 From: Trey Bastian <2991824+TreyBastian@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:57:44 +0000 Subject: refactored to hopefully make it more readable --- 2024/day_01.ml | 54 ++++++++++++++++++++++++++---------------------------- 2024/dune | 1 + 2 files changed, 27 insertions(+), 28 deletions(-) (limited to '2024') diff --git a/2024/day_01.ml b/2024/day_01.ml index 42ee50d..8b93bee 100644 --- a/2024/day_01.ml +++ b/2024/day_01.ml @@ -1,32 +1,30 @@ let filename = "./day_01_input.txt" +let try_read ic = try Some (input_line ic) with End_of_file -> None -let () = - let ic = open_in filename in +let line_to_ints s = + Str.split_delim (Str.regexp " ") s |> List.map int_of_string + +let rec read_lines left right ic = + match try_read ic with + | Some line -> ( + line_to_ints line |> fun ints -> + match ints with + | [ a; b ] -> read_lines (a :: left) (b :: right) ic + | _ -> failwith "invalid") + | None -> + close_in ic; + (List.sort compare left, List.sort compare right) - let try_read () = try Some (input_line ic) with End_of_file -> None in +let calc_sum left right = + List.map2 ( - ) left right |> List.map abs |> List.fold_left ( + ) 0 - 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 +let calc_sim left right = + List.map + (fun x -> x * (List.filter (fun y -> x == y) right |> List.length)) + left + |> List.fold_left ( + ) 0 + +let () = + let left, right = open_in filename |> read_lines [] [] in + calc_sum left right |> Printf.sprintf "Sum %d" |> print_endline; + calc_sim left right |> Printf.sprintf "Sim %d" |> print_endline diff --git a/2024/dune b/2024/dune index 2a7fc2a..7c93118 100644 --- a/2024/dune +++ b/2024/dune @@ -1,2 +1,3 @@ (executable (name day_01) (libraries str)) +(executable (name day_02)) -- cgit v1.2.3