From c6012ddce29bad059cef53b8d196d2849d874aa5 Mon Sep 17 00:00:00 2001 From: Ummon Date: Thu, 7 Dec 2017 12:45:51 +0100 Subject: [PATCH] Simplify a bit --- AdventOfCode2017/Day7.fs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 -- 2.45.2