From 137ef06d9d1fe9e0ebf3883f1eba9da7af0fa392 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 5 Dec 2024 11:26:38 +0000 Subject: [PATCH] move 2405 to use a hash table. --- bin/day2405.ml | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bin/day2405.ml b/bin/day2405.ml index fd13e25..a65cb2b 100644 --- a/bin/day2405.ml +++ b/bin/day2405.ml @@ -1,19 +1,21 @@ module IntSet = Set.Make (Int) -module IntMap = Map.Make (Int) +module IntHashtbl = Hashtbl.Make (Int) (** [add_rule a b m] adds the rule that [a] must appear before [b] to the rule - map [m]. *) -let add_rule a b = - let update_rule = function - | None -> Some (IntSet.singleton b) - | Some s -> Some (IntSet.add b s) - in - IntMap.update a update_rule + map [m]. Returns the updated map [m] *) +let add_rule a b m = + match IntHashtbl.find_opt m a with + | None -> + IntHashtbl.add m a (IntSet.singleton b); + m + | Some s -> + IntHashtbl.replace m a (IntSet.add b s); + m (** [find_rule a b m] returns [true] if the rule map [m] says that [a] should appear before [b]. *) let find_rule a b m = - match IntMap.find_opt a m with + match IntHashtbl.find_opt m a with | Some s -> ( match IntSet.find_opt b s with Some _ -> true | None -> false) | None -> false @@ -51,7 +53,7 @@ let parse_rules = | [ a; b ] -> impl (add_rule a b acc) t | _ -> failwith "parse_rules.impl") in - impl IntMap.empty + impl (IntHashtbl.create 17) (** [parse_page_orders lst] parses a list of page orders. *) let parse_page_orders =