You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
786 B
OCaml

let get_one_line file =
try Some (input_line file) with
End_of_file -> None
let get_lines file =
let rec input lines =
match get_one_line file with
Some line -> input (line :: lines)
| None -> List.rev lines
in input []
let find_pair_and_prod items total =
let len = List.length items in
let rec check idx =
let one = List.nth items (idx mod len) in
let two = List.nth items ((idx / len) mod len) in
let three = List.nth items (idx / (len * len)) in
if one + two + three == total
then one * two * three
else check (idx + 1)
in check 0
let file = "day1.txt"
let channel = open_in(file)
let lines = get_lines channel
let ints = List.map int_of_string lines
let prod = find_pair_and_prod ints 202000;;
Printf.printf "%i" prod