-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2-wrapping.ml
41 lines (38 loc) · 931 Bytes
/
day2-wrapping.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
let get_lines filename =
let rec _get_lines file acc =
try
let line = input_line file in
_get_lines file @@ line::acc
with e ->
close_in_noerr file;
List.rev acc
in
_get_lines (open_in filename) []
;;
let input = get_lines "input/day2.txt" |> List.map
(fun str -> Scanf.sscanf str "%ix%ix%i"
(fun l w h -> l, w, h)
)
;;
let wrapping lst =
let surface l w h = 2*l*w + 2*w*h + 2*h*l +
if l>=w && l>=h then w*h
else if w>=l && w>=h then l*h
else l*w
and perimeter l w h = l*w*h +
if l>=w && l>=h then 2 * (w+h)
else if w>=l && w>=h then 2 * (l+h)
else 2 * (l+w)
in
let rec _wrapping lst paper ribbon =
match lst with
| [] -> paper, ribbon
| (l,w,h)::t -> _wrapping t ( paper+(surface l w h) )( ribbon+(perimeter l w h) )
in
_wrapping lst 0 0
;;
(* Problem 1 *)
let () =
let paper, ribbon = wrapping input in
Printf.printf "Resultat pb1 : %d\nResultat pb2 : %d\n" paper ribbon
;;