projects
/
advent-of-code-19.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Day 14 done.
[advent-of-code-19.git]
/
advent03
/
src
/
advent03.hs
diff --git
a/advent03/src/advent03.hs
b/advent03/src/advent03.hs
index 2d1eef51f4e7d82af73fa819613adfefc184abfc..c022a1bb98c5dc6fae2ea663ff64c8f70f57a406 100644
(file)
--- a/
advent03/src/advent03.hs
+++ b/
advent03/src/advent03.hs
@@
-8,8
+8,7
@@
import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L
import qualified Control.Applicative as CA
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 Data.List (foldl', foldl1')
import qualified Data.Map as M
import Data.Map ((!))
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 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
}
, _tip :: Location
, _currentLength :: Int
}
@@
-47,23
+44,19
@@
part1 :: [Path] -> Int
part1 paths = closest $ crossovers paths
part2 :: [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 :: 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 =
crossovers :: [Path] -> Visited
crossovers travelledPaths =
- foldl' S.intersection
- (M.keysSet $ _visited $ head travelledPaths)
- (map (M.keysSet . _visited) $ drop 1 travelledPaths)
+ foldl1' (M.intersectionWith (+)) $ map _visited travelledPaths
travelAllPaths :: [[Segment]] -> [Path]
travelAllPaths = map travelPath
travelAllPaths :: [[Segment]] -> [Path]
travelAllPaths = map travelPath
@@
-108,7
+101,7
@@
integer = lexeme L.decimal
symb = L.symbol sc
comma = symb ","
symb = L.symbol sc
comma = symb ","
-wiresP =
some
pathP
+wiresP =
many
pathP
pathP = segmentP `sepBy1` comma
segmentP = segmentify <$> directionP <*> integer
pathP = segmentP `sepBy1` comma
segmentP = segmentify <$> directionP <*> integer