Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
drewolson committed Dec 26, 2023
1 parent 8a82537 commit 6a5b2f0
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions lib/year2023/day25.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,12 @@ let make_graph lines =
;;

let solve graph =
let rec aux (nodes, edges) =
let ln = Set.length nodes in
let le = List.length edges in
if ln = 2
then
if le = 3
then
Some (Set.fold nodes ~init:1 ~f:(fun acc node -> acc * (String.length node / 3)))
else None
else (
let n = Random.int le in
let rec aux = function
| nodes, edges when Set.length nodes = 2 && List.length edges = 3 ->
Some (Set.fold nodes ~init:1 ~f:(fun acc node -> acc * (String.length node / 3)))
| nodes, _ when Set.length nodes = 2 -> None
| nodes, edges ->
let n = edges |> List.length |> Random.int in
let a, b = List.nth_exn edges n in
let ab = a ^ b in
let edges' =
Expand All @@ -61,7 +56,7 @@ let solve graph =
| e -> Some e)
in
let nodes' = Set.add (Set.remove (Set.remove nodes a) b) ab in
aux (nodes', edges'))
aux (nodes', edges')
in
Util.Sequence.nats |> Sequence.find_map ~f:(fun _ -> aux graph) |> Option.value_exn
;;
Expand Down

0 comments on commit 6a5b2f0

Please sign in to comment.