From 52bfc1a213695ffc0a5107a0b1b7e094b5841423 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 3 Dec 2019 18:30:34 +0000 Subject: [PATCH] I remembered about foldl1 ! --- advent03/src/advent03.hs | 6 ++---- advent03/src/advent03part1.hs | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/advent03/src/advent03.hs b/advent03/src/advent03.hs index ea2645e..a863ffe 100644 --- a/advent03/src/advent03.hs +++ b/advent03/src/advent03.hs @@ -8,7 +8,7 @@ import Text.Megaparsec.Char import qualified Text.Megaparsec.Char.Lexer as L import qualified Control.Applicative as CA -import Data.List (foldl') +import Data.List (foldl', foldl1') import qualified Data.Map as M import Data.Map ((!)) @@ -56,9 +56,7 @@ shortestPaths crossings = minimum $ M.elems crossings crossovers :: [Path] -> Visited crossovers travelledPaths = - foldl' (M.intersectionWith (+)) - (_visited $ head travelledPaths) - (map _visited $ drop 1 travelledPaths) + foldl1' (M.intersectionWith (+)) $ map _visited travelledPaths travelAllPaths :: [[Segment]] -> [Path] travelAllPaths = map travelPath diff --git a/advent03/src/advent03part1.hs b/advent03/src/advent03part1.hs index 3702943..e2beb38 100644 --- a/advent03/src/advent03part1.hs +++ b/advent03/src/advent03part1.hs @@ -8,7 +8,7 @@ import Text.Megaparsec.Char import qualified Text.Megaparsec.Char.Lexer as L import qualified Control.Applicative as CA -import Data.List (foldl') +import Data.List (foldl', foldl1') import qualified Data.Set as S import Linear (V2(..), (^+^), (^-^), (*^), (*^)) @@ -46,9 +46,7 @@ closest points = S.findMin $ S.map manhattan points crossovers :: [Path] -> Visited crossovers travelledPaths = - foldl' S.intersection - (_visited $ head travelledPaths) - (map _visited $ drop 1 travelledPaths) + foldl1' S.intersection $ map _visited travelledPaths travelAllPaths :: [[Segment]] -> [Path] travelAllPaths = map travelPath -- 2.34.1