Some tidy-ups.
This commit is contained in:
@@ -1,17 +1,6 @@
|
|||||||
module StringMap = Map.Make (String)
|
module StringMap = Map.Make (String)
|
||||||
module StringSet = Set.Make (String)
|
module StringSet = Set.Make (String)
|
||||||
|
|
||||||
let[@warning "-32"] print_set set =
|
|
||||||
StringSet.to_list set |> List.sort compare |> String.concat ","
|
|
||||||
|> Printf.printf "%s\n"
|
|
||||||
|
|
||||||
let[@warning "-32"] print_sets = List.iter print_set
|
|
||||||
|
|
||||||
let[@warning "-32"] print_map =
|
|
||||||
StringMap.iter (fun k v ->
|
|
||||||
Printf.printf "%s: " k;
|
|
||||||
print_set v)
|
|
||||||
|
|
||||||
let add_connection map (a, b) =
|
let add_connection map (a, b) =
|
||||||
let update s = function
|
let update s = function
|
||||||
| None -> Some (StringSet.add s StringSet.empty)
|
| None -> Some (StringSet.add s StringSet.empty)
|
||||||
@@ -81,16 +70,14 @@ let part1 connections =
|
|||||||
find_rings [] StringSet.empty connections computers
|
find_rings [] StringSet.empty connections computers
|
||||||
|> List.filter starts_with_t
|
|> List.filter starts_with_t
|
||||||
in
|
in
|
||||||
print_sets rings;
|
string_of_int (List.length rings)
|
||||||
List.length rings
|
|
||||||
|
|
||||||
let rec search_candidate max_set connections current candidates =
|
let rec search_candidate max_set connections current candidates =
|
||||||
match StringSet.choose_opt candidates with
|
match StringSet.choose_opt candidates with
|
||||||
| None ->
|
| None ->
|
||||||
if StringSet.cardinal current > StringSet.cardinal max_set then current
|
if List.length current > List.length max_set then current else max_set
|
||||||
else max_set
|
|
||||||
| Some h ->
|
| Some h ->
|
||||||
let current' = StringSet.add h current in
|
let current' = h :: current in
|
||||||
let candidates' =
|
let candidates' =
|
||||||
StringSet.inter candidates (StringMap.find h connections)
|
StringSet.inter candidates (StringMap.find h connections)
|
||||||
in
|
in
|
||||||
@@ -101,18 +88,12 @@ let rec search_candidate max_set connections current candidates =
|
|||||||
let find_max_set connections =
|
let find_max_set connections =
|
||||||
let rec impl max_set = function
|
let rec impl max_set = function
|
||||||
| [] -> max_set
|
| [] -> max_set
|
||||||
| (k, v) :: t ->
|
| (k, v) :: t -> impl (search_candidate max_set connections [ k ] v) t
|
||||||
impl
|
|
||||||
(search_candidate max_set connections
|
|
||||||
(StringSet.add k StringSet.empty)
|
|
||||||
v)
|
|
||||||
t
|
|
||||||
in
|
in
|
||||||
impl StringSet.empty (StringMap.to_list connections)
|
impl [] (StringMap.to_list connections)
|
||||||
|
|
||||||
let part2 connections =
|
let part2 connections =
|
||||||
let max_set = find_max_set connections in
|
let max_set = find_max_set connections in
|
||||||
print_set max_set;
|
List.sort compare max_set |> String.concat ","
|
||||||
StringSet.cardinal max_set
|
|
||||||
|
|
||||||
let _ = Aoc.main load_file [ (string_of_int, part1); (string_of_int, part2) ]
|
let _ = Aoc.main load_file [ (Fun.id, part1); (Fun.id, part2) ]
|
||||||
|
Reference in New Issue
Block a user