From 465a8f21d65b320024a984103aa5f2bdbd595e77 Mon Sep 17 00:00:00 2001 From: Ummon Date: Thu, 7 Dec 2017 10:22:01 +0100 Subject: [PATCH] A bit less verbose --- AdventOfCode2017/Day7.fs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/AdventOfCode2017/Day7.fs b/AdventOfCode2017/Day7.fs index 1dd4728..e04f4ba 100644 --- a/AdventOfCode2017/Day7.fs +++ b/AdventOfCode2017/Day7.fs @@ -44,13 +44,12 @@ 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) + let towersByWeight = tower.Above |> Seq.groupBy weight |> Seq.sortBy (snd >> Seq.length) |> List.ofSeq - if towersByWeight |> Seq.length > 1 then - let unbalanced = towersByWeight |> Seq.item 0 - let others = towersByWeight |> Seq.item 1 + 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)) - else + | _ -> tower.Above |> Seq.tryPick (fun t -> findUnbalanced t) -- 2.45.2