summary refs log tree commit diff stats
path: root/day24.fsx
blob: 3acdd7be2084794efe8b9bc8caf6875c577f1a46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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"