From 38d78460fa32f19b568ed08a2a78112a9cc01102 Mon Sep 17 00:00:00 2001 From: Ummon Date: Thu, 7 Dec 2017 10:27:16 +0100 Subject: [PATCH] Less verbose... also less readable --- AdventOfCode2017/Day7.fs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/AdventOfCode2017/Day7.fs b/AdventOfCode2017/Day7.fs index e04f4ba..2d4c252 100644 --- a/AdventOfCode2017/Day7.fs +++ b/AdventOfCode2017/Day7.fs @@ -44,12 +44,6 @@ let rec findUnbalanced (tower : Tower) : (Tower * int) option = let rec weight tower = tower.Weight + (tower.Above |> Seq.map weight |> Seq.sum) - let towersByWeight = tower.Above |> Seq.groupBy weight |> Seq.sortBy (snd >> Seq.length) |> List.ofSeq - - match towersByWeight with - | unbalanced :: others :: [] -> - let delta = fst others - fst unbalanced - let unbalanced' = unbalanced |> snd |> Seq.head - findUnbalanced unbalanced' |> Option.orElse (Some (unbalanced', unbalanced'.Weight + delta)) - | _ -> - tower.Above |> Seq.tryPick (fun t -> findUnbalanced t) + 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 (fun t -> findUnbalanced t) -- 2.45.2