155 lines
5.6 KiB
OCaml
155 lines
5.6 KiB
OCaml
val ints_of_string : ?sep:string -> string -> int list
|
|
(** [nums_from_string ?sep s] takes a string of integers separated by [sep] and
|
|
gives back a list of the integers. By default [sep] is " " *)
|
|
|
|
val distance1 : int -> int -> int
|
|
(** [distance1 a b] returns the absolute difference between [a] and [b]. *)
|
|
|
|
val strings_of_file : string -> string list
|
|
(** [strings_from_file fname] returns a list of strings from the file [fname].
|
|
Each string represents a line from the file. *)
|
|
|
|
val string_of_file : string -> string
|
|
(** [string_of_file fname] returns the first line in [fname]. *)
|
|
|
|
val log10i : int -> int
|
|
(** [log10i n] returns the floor of [(log10 (float_of_int n))]. [n] must be
|
|
positive. *)
|
|
|
|
val digits10 : int -> int
|
|
(** [digits10 n] returns the number of base-10 digits in [n]. *)
|
|
|
|
val pow10 : int -> int
|
|
(** [pow10 n] returns [int_of_float (10. ** float_of_int n)]. [n] must be
|
|
non-negative. *)
|
|
|
|
val main : (string -> 'a) -> (('b -> string) * ('a -> 'b)) list -> unit
|
|
(** [main prep parts] executes an advent of code problem. [prep fname] should be
|
|
a function that returns the input from [fname]. Each elemet of [parts] is a
|
|
pair of functions. The first converts the output to a string (for example
|
|
[string_of_int]). The second executes the given part. Output is given as if
|
|
done by: [print_string ( prep fname |> snd |> fst )] *)
|
|
|
|
val memoize : ('a, 'b) Hashtbl.t -> ('a -> 'b) -> 'a -> 'b
|
|
(** [memoize memo f value] returns the result of [f value]. The hashtable [memo]
|
|
is used to cache results, so repeated calls with the same [value] will not
|
|
call [f] again. *)
|
|
|
|
val apply_n : int -> ('a -> 'a) -> 'a -> 'a
|
|
(** [apply_n n fn arg] is equivalent to [(fn (fn ... (fn (fn arg))))] where [fn]
|
|
is called [n] times.*)
|
|
|
|
(** Module representing a pair of integers, useful for Set.Make *)
|
|
module IntPair : sig
|
|
type t = int * int
|
|
(** [t] is a pair of integers *)
|
|
|
|
val compare : t -> t -> int
|
|
(** Standard comparion operation. *)
|
|
end
|
|
|
|
(** IntPairSet represents a Set of integers, see standard docs for info. *)
|
|
module IntPairSet : sig
|
|
type elt = IntPair.t
|
|
type t = Set.Make(IntPair).t
|
|
|
|
val empty : t
|
|
val add : elt -> t -> t
|
|
val singleton : elt -> t
|
|
val remove : elt -> t -> t
|
|
val union : t -> t -> t
|
|
val inter : t -> t -> t
|
|
val disjoint : t -> t -> bool
|
|
val diff : t -> t -> t
|
|
val cardinal : t -> int
|
|
val elements : t -> elt list
|
|
val min_elt : t -> elt
|
|
val min_elt_opt : t -> elt option
|
|
val max_elt : t -> elt
|
|
val max_elt_opt : t -> elt option
|
|
val choose : t -> elt
|
|
val choose_opt : t -> elt option
|
|
val find : elt -> t -> elt
|
|
val find_opt : elt -> t -> elt option
|
|
val find_first : (elt -> bool) -> t -> elt
|
|
val find_first_opt : (elt -> bool) -> t -> elt option
|
|
val find_last : (elt -> bool) -> t -> elt
|
|
val find_last_opt : (elt -> bool) -> t -> elt option
|
|
val iter : (elt -> unit) -> t -> unit
|
|
val fold : (elt -> 'acc -> 'acc) -> t -> 'acc -> 'acc
|
|
val map : (elt -> elt) -> t -> t
|
|
val filter : (elt -> bool) -> t -> t
|
|
val filter_map : (elt -> elt option) -> t -> t
|
|
val partition : (elt -> bool) -> t -> t * t
|
|
val split : elt -> t -> t * bool * t
|
|
val is_empty : t -> bool
|
|
val mem : elt -> t -> bool
|
|
val equal : t -> t -> bool
|
|
val compare : t -> t -> int
|
|
val subset : t -> t -> bool
|
|
val for_all : (elt -> bool) -> t -> bool
|
|
val exists : (elt -> bool) -> t -> bool
|
|
val to_list : t -> elt list
|
|
val of_list : elt list -> t
|
|
val to_seq_from : elt -> t -> elt Seq.t
|
|
val to_seq : t -> elt Seq.t
|
|
val to_rev_seq : t -> elt Seq.t
|
|
val add_seq : elt Seq.t -> t -> t
|
|
val of_seq : elt Seq.t -> t
|
|
end
|
|
|
|
(** The [Grid] module is used to represent and manipulate a grid of characters.
|
|
Its main goals are to be non-mutable and have constant access times to
|
|
locations in the grid.
|
|
|
|
Grid locations can be accessed by index or (col, row) position. Indicies do
|
|
not guarantee an ordering on accesses - but iterating by index from 0 to
|
|
[Grid.length grid - 1] inclusive will cover the whole grid. *)
|
|
module Grid : sig
|
|
type t
|
|
(** The type used to represent a grid *)
|
|
|
|
val of_file : string -> t
|
|
(** [Grid.of_file fname] returns a grid loaded from the file [fname] *)
|
|
|
|
val length : t -> int
|
|
(** [Grid.length grid] returns the length of the grid. *)
|
|
|
|
val get_by_idx : t -> int -> char
|
|
(** [Grid.get_by_idx grid idx] returns the character at index [idx] in [grid].
|
|
*)
|
|
|
|
val get_by_pos : t -> int * int -> char
|
|
(** [Grid.get_by_pos grid pos] returns the character at position [pos] in
|
|
[grid]. *)
|
|
|
|
val get_by_pos_opt : t -> int * int -> char option
|
|
(** [Grid.get_by_pos_opt grid pos] returns [Some (get_by_pos grid pos)] if
|
|
[pos] is a valid position in [grid], and [None] otherwise. *)
|
|
|
|
val pos_of_idx : t -> int -> int * int
|
|
(** [Grid.pos_of_idx grid idx] returns the [(x, y)] position mapped by [idx]
|
|
in [grid]. *)
|
|
|
|
val idx_of_pos : t -> int * int -> int
|
|
(** [Grid.pos_of_idx grid pos] returns the index corresponding to [pos] in
|
|
[grid]. *)
|
|
|
|
val pos_is_valid : t -> int * int -> bool
|
|
(** [Grid.pos_is_valid grid pos] returns [true] if and only if [pos] is a
|
|
valid position in [grid]. *)
|
|
|
|
val idx_from_opt : t -> int -> char -> int option
|
|
(** [Grid.idx_from_opt grid start c] returns [Some idx] where [idx] is the
|
|
first location in [grid] at or after the [start] index which is [c]. It
|
|
returns [None] if [c] does not appear. *)
|
|
|
|
val update_pos : t -> int * int -> char -> t
|
|
(** [Grid.update_pos grid pos c] returns a grid with the character at position
|
|
[pos] changed to [c]. *)
|
|
|
|
val update_idx : t -> int -> char -> t
|
|
(** [Grid.update_pos grid idx c] returns a grid with the character at index
|
|
[idx] changed to [c]. *)
|
|
end
|