2024 day 3 part 1 looks reasonable now.
This commit is contained in:
@@ -20,25 +20,24 @@ let load_file fname =
|
|||||||
|> List.map make_pairs
|
|> List.map make_pairs
|
||||||
|> List.fold_left add_connection StringMap.empty
|
|> List.fold_left add_connection StringMap.empty
|
||||||
|
|
||||||
let rec find_second_member acc connections visited a =
|
let rec find_second_member acc connections visited a candidates =
|
||||||
let rec impl acc a b = function
|
let rec impl acc set = function
|
||||||
| [] -> acc
|
| [] -> acc
|
||||||
| c :: t -> impl (StringSet.of_list [ a; b; c ] :: acc) a b t
|
| c :: t -> impl (StringSet.add c set :: acc) set t
|
||||||
in
|
in
|
||||||
function
|
match StringSet.choose_opt candidates with
|
||||||
| [] -> acc
|
| None -> acc
|
||||||
| h :: t ->
|
| Some h ->
|
||||||
|
let candidates = StringSet.remove h candidates in
|
||||||
if StringSet.mem h visited then
|
if StringSet.mem h visited then
|
||||||
find_second_member acc connections visited a t
|
find_second_member acc connections visited a candidates
|
||||||
else
|
else
|
||||||
let visited = StringSet.add h visited in
|
let visited = StringSet.add h visited in
|
||||||
let anh =
|
let anh = StringSet.inter (StringMap.find h connections) candidates in
|
||||||
StringSet.inter
|
let acc =
|
||||||
(StringMap.find a connections)
|
impl acc (StringSet.of_list [ a; h ]) (StringSet.to_list anh)
|
||||||
(StringMap.find h connections)
|
|
||||||
in
|
in
|
||||||
let acc = impl acc a h (StringSet.to_list anh) in
|
find_second_member acc connections visited a candidates
|
||||||
find_second_member acc connections visited a t
|
|
||||||
|
|
||||||
let rec find_rings acc visited connections = function
|
let rec find_rings acc visited connections = function
|
||||||
| [] -> acc
|
| [] -> acc
|
||||||
@@ -48,7 +47,7 @@ let rec find_rings acc visited connections = function
|
|||||||
let visited = StringSet.add h visited in
|
let visited = StringSet.add h visited in
|
||||||
let acc =
|
let acc =
|
||||||
find_second_member acc connections visited h
|
find_second_member acc connections visited h
|
||||||
(StringSet.to_list (StringMap.find h connections))
|
(StringMap.find h connections)
|
||||||
in
|
in
|
||||||
find_rings acc visited connections t
|
find_rings acc visited connections t
|
||||||
|
|
||||||
@@ -56,14 +55,11 @@ let rec find_rings acc visited connections = function
|
|||||||
letter ['t']. *)
|
letter ['t']. *)
|
||||||
let starts_with_t set = StringSet.exists (fun x -> x.[0] = 't') set
|
let starts_with_t set = StringSet.exists (fun x -> x.[0] = 't') set
|
||||||
|
|
||||||
let set_compare a b = compare (StringSet.elements a) (StringSet.elements b)
|
|
||||||
|
|
||||||
let part1 connections =
|
let part1 connections =
|
||||||
StringMap.to_list connections
|
StringMap.to_list connections
|
||||||
|> List.map fst
|
|> List.map fst
|
||||||
|> find_rings [] StringSet.empty connections
|
|> find_rings [] StringSet.empty connections
|
||||||
|> List.filter starts_with_t |> List.sort_uniq set_compare |> List.length
|
|> List.filter starts_with_t |> List.length |> string_of_int
|
||||||
|> string_of_int
|
|
||||||
|
|
||||||
(** [search_candidate max_lst connections current candidates] searches for the
|
(** [search_candidate max_lst connections current candidates] searches for the
|
||||||
longest star network that contains all the computers in [current] and maybe
|
longest star network that contains all the computers in [current] and maybe
|
||||||
|
Reference in New Issue
Block a user