Files
ocaml-aoc/lib/aoc.ml
Matthew Gretton-Dann 831bbf4f63 Move IntPair and PosSet into Aoc library.
These are obviously generic enough and useful for the future that they
can be moved to a shared library.
2024-12-06 17:15:37 +00:00

35 lines
865 B
OCaml

let ints_of_string ?(sep = " ") s =
List.map int_of_string (Str.split (Str.regexp sep) s)
let distance1 a b = abs (a - b)
let strings_of_file fname =
In_channel.with_open_text fname In_channel.input_lines
let main prep parts =
try
match Sys.argv with
| [| _; fname |] ->
let lines = prep fname in
let do_part i (fmt, fn) =
Printf.printf "Part %d = %s\n" (i + 1) (fmt (fn lines));
flush stdout
in
List.iteri do_part parts;
exit 0
| _ ->
Printf.printf "Usage: %s <fname>\n" Sys.executable_name;
exit 2
with e ->
Printf.printf "An error occured: %s\n" (Printexc.to_string e);
exit 1
module IntPair = struct
type t = int * int
let compare (x, y) (x', y') =
match compare y y' with 0 -> compare x x' | c -> c
end
module IntPairSet = Set.Make (IntPair)