From fe417025bde01d2dc0660e9a1581c3ffec8a5c96 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 11 Jan 2023 22:25:41 +0100 Subject: [PATCH] Day 1 part 2 --- ocaml/1.ml | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ocaml/1.ml b/ocaml/1.ml index 4b4017a..ddba180 100644 --- a/ocaml/1.ml +++ b/ocaml/1.ml @@ -1,12 +1,19 @@ #load "str.cma" ;; #use "lib/file_utils.ml" ;; +let top n calories = + let topn = List.filteri (fun i _ -> i < n) calories in + let numcal = List.fold_left (+) 0 topn in + Printf.printf "Top %d: %d\n" n numcal +;; + +let order_desc a b = if a < b then 1 else -1 in let contents = read_file "inputs/1.txt" in -let maxcal = Str.split (Str.regexp "\n\n") contents +let calories = 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 + |> List.sort order_desc +in +top 1 calories; +top 3 calories