Move memoize to the Aoc library.
This commit is contained in:
@@ -14,17 +14,6 @@ let towels_of_file fname = Aoc.strings_of_file fname |> towels_of_strings
|
|||||||
(** Memoizing hash table shared between parts 1 and 2. *)
|
(** Memoizing hash table shared between parts 1 and 2. *)
|
||||||
let memo = Hashtbl.create 1000
|
let memo = Hashtbl.create 1000
|
||||||
|
|
||||||
(** [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. *)
|
|
||||||
let memoize memo f value =
|
|
||||||
match Hashtbl.find_opt memo value with
|
|
||||||
| Some x -> x
|
|
||||||
| None ->
|
|
||||||
let x = f value in
|
|
||||||
Hashtbl.add memo value x;
|
|
||||||
x
|
|
||||||
|
|
||||||
(** [count_hashes memo towels pattern] counts the number of ways of matching
|
(** [count_hashes memo towels pattern] counts the number of ways of matching
|
||||||
[pattern] using [towels]. [memo] is a hashtable used for memoizing results.
|
[pattern] using [towels]. [memo] is a hashtable used for memoizing results.
|
||||||
*)
|
*)
|
||||||
@@ -35,7 +24,7 @@ let rec count_matches memo towels pattern =
|
|||||||
| h :: t ->
|
| h :: t ->
|
||||||
let towel_len = String.length h in
|
let towel_len = String.length h in
|
||||||
if String.starts_with ~prefix:h pattern then
|
if String.starts_with ~prefix:h pattern then
|
||||||
memoize memo
|
Aoc.memoize memo
|
||||||
(count_matches memo towels)
|
(count_matches memo towels)
|
||||||
(String.sub pattern towel_len (pattern_len - towel_len))
|
(String.sub pattern towel_len (pattern_len - towel_len))
|
||||||
+ count_matched t
|
+ count_matched t
|
||||||
@@ -44,12 +33,12 @@ let rec count_matches memo towels pattern =
|
|||||||
if pattern_len = 0 then 1 else count_matched towels
|
if pattern_len = 0 then 1 else count_matched towels
|
||||||
|
|
||||||
let part1 (towels, patterns) =
|
let part1 (towels, patterns) =
|
||||||
List.map (count_matches memo towels) patterns
|
List.map (Aoc.memoize memo (count_matches memo towels)) patterns
|
||||||
|> List.filter (( > ) 0)
|
|> List.filter (( < ) 0)
|
||||||
|> List.length
|
|> List.length
|
||||||
|
|
||||||
let part2 (towels, patterns) =
|
let part2 (towels, patterns) =
|
||||||
List.map (memoize memo (count_matches memo towels)) patterns
|
List.map (Aoc.memoize memo (count_matches memo towels)) patterns
|
||||||
|> List.fold_left ( + ) 0
|
|> List.fold_left ( + ) 0
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
|
@@ -90,3 +90,11 @@ let pow10 n =
|
|||||||
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
|
let rec impl acc = function 0 -> acc | x -> impl (acc * 10) (x - 1) in
|
||||||
assert (n >= 0);
|
assert (n >= 0);
|
||||||
impl 1 n
|
impl 1 n
|
||||||
|
|
||||||
|
let memoize memo f value =
|
||||||
|
match Hashtbl.find_opt memo value with
|
||||||
|
| Some x -> x
|
||||||
|
| None ->
|
||||||
|
let x = f value in
|
||||||
|
Hashtbl.add memo value x;
|
||||||
|
x
|
||||||
|
@@ -30,6 +30,11 @@ val main : (string -> 'a) -> (('b -> string) * ('a -> 'b)) list -> unit
|
|||||||
[string_of_int]). The second executes the given part. Output is given as if
|
[string_of_int]). The second executes the given part. Output is given as if
|
||||||
done by: [print_string ( prep fname |> snd |> fst )] *)
|
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. *)
|
||||||
|
|
||||||
(** Module representing a pair of integers, useful for Set.Make *)
|
(** Module representing a pair of integers, useful for Set.Make *)
|
||||||
module IntPair : sig
|
module IntPair : sig
|
||||||
type t = int * int
|
type t = int * int
|
||||||
|
Reference in New Issue
Block a user