1
0
Fork 0

Simplify/refactor day 1 code

This commit is contained in:
Gregory Eremin 2023-01-11 13:35:10 +01:00
parent bf692a17db
commit 8a12f5aac1
2 changed files with 9 additions and 11 deletions

View File

@ -1,20 +1,12 @@
#load "str.cma" ;; #load "str.cma" ;;
#use "lib/file_utils.ml" ;;
let read_file filename = let contents = read_file "inputs/1.txt" in
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 maxcal = Str.split (Str.regexp "\n\n") contents let maxcal = 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.fold_left (max) 0
;; in
Printf.printf "%d" maxcal Printf.printf "%d" maxcal

6
ocaml/lib/file_utils.ml Normal file
View File

@ -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
;;