From: Neil Smith Date: Tue, 3 Dec 2019 17:33:13 +0000 (+0000) Subject: Now using M.intersectionWith, so no need for Data.Set X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=1915ebe9124a2075ed2212a6817f3e441c0c36fb;p=advent-of-code-19.git Now using M.intersectionWith, so no need for Data.Set --- diff --git a/advent03/src/advent03.hs b/advent03/src/advent03.hs index 2d1eef5..ea2645e 100644 --- a/advent03/src/advent03.hs +++ b/advent03/src/advent03.hs @@ -9,7 +9,6 @@ import qualified Text.Megaparsec.Char.Lexer as L import qualified Control.Applicative as CA import Data.List (foldl') -import qualified Data.Set as S import qualified Data.Map as M import Data.Map ((!)) @@ -19,11 +18,9 @@ data Direction = East | South | West | North deriving (Show, Eq) type Location = V2 Int -- x, y -type Visited = S.Set Location +type Visited = M.Map Location Int -type TrackedVisited = M.Map Location Int - -data Path = Path { _visited :: TrackedVisited +data Path = Path { _visited :: Visited , _tip :: Location , _currentLength :: Int } @@ -47,23 +44,21 @@ part1 :: [Path] -> Int part1 paths = closest $ crossovers paths part2 :: [Path] -> Int -part2 paths = shortestPaths paths $ crossovers paths +part2 paths = shortestPaths $ crossovers paths closest :: Visited -> Int -closest points = S.findMin $ S.map manhattan points - +closest points = snd $ M.findMin $ M.mapWithKey (\k _ -> manhattan k) points -shortestPaths :: [Path] -> Visited -> Int -shortestPaths paths crossings = minimum $ S.map crossingPathLengths crossings - where crossingPathLengths crossing = sum $ map (\p -> (_visited p)!crossing) paths +shortestPaths :: Visited -> Int +shortestPaths crossings = minimum $ M.elems crossings crossovers :: [Path] -> Visited crossovers travelledPaths = - foldl' S.intersection - (M.keysSet $ _visited $ head travelledPaths) - (map (M.keysSet . _visited) $ drop 1 travelledPaths) + foldl' (M.intersectionWith (+)) + (_visited $ head travelledPaths) + (map _visited $ drop 1 travelledPaths) travelAllPaths :: [[Segment]] -> [Path] travelAllPaths = map travelPath