1
0
Fork 0

Day 1 part 2

This commit is contained in:
Gregory Eremin 2023-01-11 22:25:41 +01:00
parent d924d15dd6
commit fe417025bd
1 changed files with 12 additions and 5 deletions

View File

@ -1,12 +1,19 @@
#load "str.cma" ;; #load "str.cma" ;;
#use "lib/file_utils.ml" ;; #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 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 (String.split_on_char '\n')
|> List.map (List.map (int_of_string)) |> List.map (List.map (int_of_string))
|> List.map (List.fold_left (+) 0) |> List.map (List.fold_left (+) 0)
|> List.fold_left (max) 0 |> List.sort order_desc
in in
top 1 calories;
Printf.printf "%d" maxcal top 3 calories