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.

30 lines
713 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 left = List.nth items (idx mod len) in
let right = List.nth items (idx / len) in
if left + right == total
then left * right
else check (idx+1)
in check 0
let file = "day1_1.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 2020;;
Printf.printf "%i" prod