36 lines
955 B
OCaml
36 lines
955 B
OCaml
module IntSet = Set.Make(Int)
|
|
|
|
let read_ints =
|
|
Bracket.infile_lines ~line:int_of_string ~of_seq:IntSet.of_seq
|
|
|
|
let find_with target ints =
|
|
let open IntSet in
|
|
let p x = mem (target - x) ints in
|
|
let res = min_elt (filter p ints) in
|
|
res, target - res
|
|
|
|
let find = find_with 2020
|
|
|
|
let main_part_1 input =
|
|
let ints = read_ints input in
|
|
let x, y = find ints in
|
|
Printf.printf "%d * %d = %d\n" x y (x * y)
|
|
|
|
let main_part_2 input =
|
|
let ints = read_ints input in
|
|
let exception Found of int * int * int in
|
|
let go x =
|
|
match find_with (2020 - x) ints with
|
|
| exception Not_found -> ()
|
|
| y, z -> raise (Found (x, y, z))
|
|
in
|
|
match IntSet.iter go ints with
|
|
| exception Found (x, y, z) ->
|
|
Printf.printf "%d * %d * %d = %d\n" x y z (x * y * z)
|
|
| _ -> raise Not_found
|
|
|
|
let mains = [|main_part_1; main_part_2|]
|
|
|
|
let main = function
|
|
| [part; input] -> mains.(int_of_string part - 1) input
|
|
| _ -> Usage.exit "1 <part> <infile>"
|