2023-01-10 16:23:37 +00:00
|
|
|
#load "str.cma" ;;
|
2023-01-11 12:35:10 +00:00
|
|
|
#use "lib/file_utils.ml" ;;
|
2023-01-10 16:23:37 +00:00
|
|
|
|
2023-01-11 21:25:41 +00:00
|
|
|
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
|
2023-01-11 12:35:10 +00:00
|
|
|
let contents = read_file "inputs/1.txt" in
|
2023-01-11 21:25:41 +00:00
|
|
|
let calories = Str.split (Str.regexp "\n\n") contents
|
2023-01-10 16:23:37 +00:00
|
|
|
|> List.map (String.split_on_char '\n')
|
|
|
|
|> List.map (List.map (int_of_string))
|
|
|
|
|> List.map (List.fold_left (+) 0)
|
2023-01-11 21:25:41 +00:00
|
|
|
|> List.sort order_desc
|
|
|
|
in
|
|
|
|
top 1 calories;
|
|
|
|
top 3 calories
|