Update to use Aoc.pow10 and Aoc.digits10.
This commit is contained in:
@@ -1,20 +1,6 @@
|
|||||||
let ints_of_file fname =
|
let ints_of_file fname =
|
||||||
Aoc.strings_of_file fname |> List.map (Aoc.ints_of_string ~sep:"[: ]+")
|
Aoc.strings_of_file fname |> List.map (Aoc.ints_of_string ~sep:"[: ]+")
|
||||||
|
|
||||||
(** [log10i i] returns the integer part of [log10 i]. [i] must be greater than
|
|
||||||
zero. *)
|
|
||||||
let log10i i =
|
|
||||||
let rec impl acc = function 0 -> acc | x -> impl (acc + 1) (x / 10) in
|
|
||||||
assert (i > 0);
|
|
||||||
impl ~-1 i
|
|
||||||
|
|
||||||
(** [pow10 n] returns [10] raised to the [n]th power. [n] must be non-negative.
|
|
||||||
*)
|
|
||||||
let pow10 n =
|
|
||||||
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
|
|
||||||
assert (n >= 0);
|
|
||||||
impl 1 n
|
|
||||||
|
|
||||||
(** [check_add tgt v] Check to see if [X + v = tgt] is a valid operation. If not
|
(** [check_add tgt v] Check to see if [X + v = tgt] is a valid operation. If not
|
||||||
returns [None] otherwise returns [Some X]. *)
|
returns [None] otherwise returns [Some X]. *)
|
||||||
let check_add tgt v = if v > tgt then None else Some (tgt - v)
|
let check_add tgt v = if v > tgt then None else Some (tgt - v)
|
||||||
@@ -26,7 +12,7 @@ let check_mul tgt v = if tgt mod v = 0 then Some (tgt / v) else None
|
|||||||
(** [check_cat tgt v] Check to see if [X || v = tgt] is a valid operation. If
|
(** [check_cat tgt v] Check to see if [X || v = tgt] is a valid operation. If
|
||||||
not returns [None] otherwise returns [Some X]. *)
|
not returns [None] otherwise returns [Some X]. *)
|
||||||
let check_cat tgt v =
|
let check_cat tgt v =
|
||||||
let p = pow10 (1 + log10i v) in
|
let p = Aoc.pow10 (Aoc.digits10 v) in
|
||||||
if tgt mod p = v then Some (tgt / p) else None
|
if tgt mod p = v then Some (tgt / p) else None
|
||||||
|
|
||||||
(** [is_valid_target tgt nums ops] returns [true] if we can reach [tgt] from
|
(** [is_valid_target tgt nums ops] returns [true] if we can reach [tgt] from
|
||||||
|
Reference in New Issue
Block a user