Add pow10, log10i and digits10 functions
These are used by a couple of solutions now.
This commit is contained in:
15
lib/aoc.ml
15
lib/aoc.ml
@@ -66,3 +66,18 @@ module Grid = struct
|
||||
(String.sub grid.grid (idx + 1) (length grid - idx - 1));
|
||||
{ grid with grid = Buffer.contents builder }
|
||||
end
|
||||
|
||||
let log10i i =
|
||||
let rec impl acc = function 0 -> acc | x -> impl (acc + 1) (x / 10) in
|
||||
assert (i > 0);
|
||||
impl ~-1 i
|
||||
|
||||
let digits10 = function
|
||||
| 0 -> 1
|
||||
| n when n > 0 -> 1 + log10i n
|
||||
| n (* when n < 0 *) -> 1 + log10i (-n)
|
||||
|
||||
let pow10 n =
|
||||
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
|
||||
assert (n >= 0);
|
||||
impl 1 n
|
||||
|
11
lib/aoc.mli
11
lib/aoc.mli
@@ -9,6 +9,17 @@ 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 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
|
||||
|
Reference in New Issue
Block a user