open System.IO let parseLine (str: string) = str.Split('/') |> Array.map int |> (fun x -> (x[0], x[1])) let strength = List.sumBy (fun c -> fst c + snd c) let edges = File.ReadAllLines "day24.txt" |> Array.map parseLine let rec build bridge next components = seq { yield bridge if Set.contains (next, next) components then yield! build ((next, next) :: bridge) next (Set.remove (next, next) components) else let bridgeable = Set.filter (fun c -> fst c = next || snd c = next) components for comp in bridgeable do let next' = if snd comp = next then fst comp else snd comp yield! build (comp :: bridge) next' (Set.remove comp components) } let solve maximiser = set >> build [] 0 >> Seq.maxBy maximiser >> strength solve strength edges |> printfn "%A" solve (fun c -> (List.length c, strength c)) edges |> printfn "%A"