forked from jO-Osko/AdventOfCodeOcaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_windows.ml
More file actions
83 lines (67 loc) · 2.23 KB
/
project_windows.ml
File metadata and controls
83 lines (67 loc) · 2.23 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
let preberi_datoteko ime_datoteke =
let chan = open_in ime_datoteke in
let vsebina = really_input_string chan (in_channel_length chan) in
close_in chan;
vsebina
let izpisi_datoteko ime_datoteke vsebina =
let chan = open_out ime_datoteke in
output_string chan vsebina;
close_out chan
module List = struct
include List
let int_list l = List.map int_of_string l
let sum l =
let rec sum' a = function [] -> a | x :: xs -> sum' (a + x) xs in
sum' 0 l
let lines = String.split_on_char '\n'
end
module type Solver = sig
val naloga1 : string -> string
val naloga2 : string -> string -> string
end
module Solver0 : Solver = struct
let cost_fun x = (x / 3) - 2
let rec full_cost x =
let c_cost = cost_fun x in
if c_cost <= 0 then 0 else c_cost + full_cost c_cost
let naloga1 data =
let lines = List.lines data in
lines |> List.int_list
|> List.fold_left (fun s x -> s + cost_fun x) 0
|> string_of_int
let naloga2 data _part1 =
data |> List.lines |> List.int_list |> List.map full_cost |> List.sum
|> string_of_int
end
(* Tukaj re-definirajte funkcijo naloga1 in naloga2 *)
module Solver1 : Solver = struct
let naloga1 data = ""
let naloga2 data _part1 = ""
end
(* Poženemo zadevo *)
let choose_solver : string -> (module Solver) = function
| "0" -> (module Solver0)
| "1" -> (module Solver1)
| _ -> failwith "Ni še rešeno"
let main () =
let day = Sys.argv.(1) in
print_endline ("Solving DAY: " ^ day);
let (module Solver) = choose_solver day in
let input_data = preberi_datoteko ("data/day_" ^ day ^ ".in") in
let p1_start = Sys.time () in
let part1 = Solver.naloga1 input_data in
let t1_time = Sys.time () -. p1_start in
print_endline "PART 1:";
print_endline part1;
print_endline ("Taken: " ^ string_of_float t1_time ^ "s");
let p2_start = Sys.time () in
let part2 = Solver.naloga2 input_data part1 in
let t2_time = Sys.time () -. p2_start in
print_endline "PART 2:";
print_endline part2;
print_endline ("Taken: " ^ string_of_float t2_time ^ "s");
print_endline ("Total: " ^ string_of_float (t1_time +. t2_time) ^ "s");
izpisi_datoteko ("out/day_" ^ day ^ "_1.out") part1;
izpisi_datoteko ("out/day_" ^ day ^ "_2.out") part2;
()
let _ = main ()