)
let buildTower (input : Input) : Tower =
- let rootTowers = Dictionary<string, Tower> ()
+ let towers = Dictionary<string, Tower> ()
for tower, _ in input do
- rootTowers.Add (tower.Name, tower)
+ towers.Add (tower.Name, tower)
for tower, towersAbove in input do
for towerAbove in towersAbove do
- tower.Above.Add rootTowers.[towerAbove]
- rootTowers.Remove towerAbove |> ignore
+ tower.Above.Add towers.[towerAbove]
+ towers.Remove towerAbove |> ignore
- rootTowers.First().Value
+ towers.First().Value
// Returns the tower and its corrected weight.
let rec findUnbalanced (tower : Tower) : (Tower * int) option =
tower.Weight + (tower.Above |> Seq.map weight |> Seq.sum)
match tower.Above |> List.ofSeq |> List.groupBy weight |> List.sortBy (snd >> List.length) with
- | [ w1, [ unbalanced ]; w2, _ ] -> findUnbalanced unbalanced |> Option.orElse (Some (unbalanced, unbalanced.Weight + w2 - w1))
- | _ -> tower.Above |> Seq.tryPick findUnbalanced
+ | [ w1, [ unbalanced ]; w2, _ ] ->
+ findUnbalanced unbalanced |> Option.orElse (Some (unbalanced, unbalanced.Weight + w2 - w1))
+ | _ ->
+ None
\ No newline at end of file