From be7302a45e659a768be47e8435f863e6f19597a2 Mon Sep 17 00:00:00 2001 From: Ummon Date: Fri, 8 Dec 2017 11:18:13 +0100 Subject: [PATCH] Replace map + sum by sumBy --- AdventOfCode2017/Day2.fs | 7 ++----- AdventOfCode2017/Day3.fs | 3 +-- AdventOfCode2017/Day7.fs | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/AdventOfCode2017/Day2.fs b/AdventOfCode2017/Day2.fs index c92fe54..bd91ef4 100644 --- a/AdventOfCode2017/Day2.fs +++ b/AdventOfCode2017/Day2.fs @@ -7,13 +7,11 @@ let parseInput (str : string) : int[][] = |> Array.map (fun line -> line.Split ([| ' '; '\t' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int) let checksum1 (a : int[][]) = - a - |> Array.map (fun ns -> Array.max ns - Array.min ns) - |> Array.sum + a |> Array.sumBy (fun ns -> Array.max ns - Array.min ns) let checksum2 (a : int[][]) = a - |> Array.map ( + |> Array.sumBy ( fun ns -> seq { for a in ns do @@ -21,4 +19,3 @@ let checksum2 (a : int[][]) = if a <> b && a % b = 0 then yield a / b } |> Seq.head ) - |> Array.sum diff --git a/AdventOfCode2017/Day3.fs b/AdventOfCode2017/Day3.fs index 3342111..71e57b6 100644 --- a/AdventOfCode2017/Day3.fs +++ b/AdventOfCode2017/Day3.fs @@ -21,8 +21,7 @@ let spiralAdjacentSumBiggerThan (n : int) = let neighborsSum (dic : Map) (pos : int * int) = let x, y = pos [ for dx in -1 .. 1 do for dy in -1 .. 1 -> x + dx, y + dy ] - |> List.map (fun (x, y) -> match dic |> Map.tryFind (x, y) with Some v -> v | None -> 0) - |> List.sum + |> List.sumBy (fun (x, y) -> match dic |> Map.tryFind (x, y) with Some v -> v | None -> 0) spiral |> Seq.skip 1 diff --git a/AdventOfCode2017/Day7.fs b/AdventOfCode2017/Day7.fs index 459f563..6f312f7 100644 --- a/AdventOfCode2017/Day7.fs +++ b/AdventOfCode2017/Day7.fs @@ -37,7 +37,7 @@ let buildTower (input : Input) : Tower = // Returns the tower and its corrected weight. let rec findUnbalanced (tower : Tower) : (Tower * int) option = let rec weight tower = - tower.Weight + (tower.Above |> Seq.map weight |> Seq.sum) + tower.Weight + (tower.Above |> Seq.sumBy weight) match tower.Above |> List.ofSeq |> List.groupBy weight |> List.sortBy (snd >> List.length) with | [ w1, [ unbalanced ]; w2, _ ] -> -- 2.45.2