From: Ummon Date: Thu, 7 Dec 2017 11:45:51 +0000 (+0100) Subject: Simplify a bit X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=c6012ddce29bad059cef53b8d196d2849d874aa5;p=advent_of_code_2017.git Simplify a bit --- diff --git a/AdventOfCode2017/Day7.fs b/AdventOfCode2017/Day7.fs index 2fe6ae0..459f563 100644 --- a/AdventOfCode2017/Day7.fs +++ b/AdventOfCode2017/Day7.fs @@ -22,17 +22,17 @@ let parseInput (lines : string list) : Input = ) let buildTower (input : Input) : Tower = - let rootTowers = Dictionary () + let towers = Dictionary () 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 = @@ -40,5 +40,7 @@ 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