From 8a12f5aac1242f5ed2391f524a9161d65eccbd56 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 11 Jan 2023 13:35:10 +0100 Subject: [PATCH] Simplify/refactor day 1 code --- ocaml/1.ml | 14 +++----------- ocaml/lib/file_utils.ml | 6 ++++++ 2 files changed, 9 insertions(+), 11 deletions(-) create mode 100644 ocaml/lib/file_utils.ml diff --git a/ocaml/1.ml b/ocaml/1.ml index 4753bc3..4b4017a 100644 --- a/ocaml/1.ml +++ b/ocaml/1.ml @@ -1,20 +1,12 @@ #load "str.cma" ;; +#use "lib/file_utils.ml" ;; -let read_file filename = - let ch = open_in filename in - let s = really_input_string ch (in_channel_length ch) in - close_in ch; - s - ;; - -let contents = read_file "inputs/1.txt" ;; -(* print_endline contents ;; *) - +let contents = read_file "inputs/1.txt" in let maxcal = Str.split (Str.regexp "\n\n") contents |> List.map (String.split_on_char '\n') |> List.map (List.map (int_of_string)) |> List.map (List.fold_left (+) 0) |> List.fold_left (max) 0 - ;; + in Printf.printf "%d" maxcal diff --git a/ocaml/lib/file_utils.ml b/ocaml/lib/file_utils.ml new file mode 100644 index 0000000..9feeb39 --- /dev/null +++ b/ocaml/lib/file_utils.ml @@ -0,0 +1,6 @@ +let read_file filename = + let ch = open_in filename in + let s = really_input_string ch (in_channel_length ch) in + close_in ch; + s + ;;