text <- TIO.readFile "data/advent06.txt"
let coords = successfulParse text
let boundingBox = findBounds coords
- print $ length coords
- print boundingBox
print $ part1 coords boundingBox
print $ part2 coords boundingBox
then tieLabel
else snd (distances!!0)
-
safeCells :: [Coord] -> Bounds -> Region
safeCells coords (minX, maxX, minY, maxY) = M.fromList distanceCells
where cells = [(x, y) | x <- [minX .. maxX], y <- [minY .. maxY] ]
distanceCells = map (\c -> (c, fromIntegral $ sumDistance c coords) ) cells
-
sumDistance :: Coord -> [Coord] -> Integer
sumDistance here others = sum $ map (\c -> distance here c) others
-
infinite :: Region -> Bounds -> [Int]
infinite regions (minX, maxX, minY, maxY) = nub $ sort $ M.elems $ M.filterWithKey onEdge regions
where onEdge (x, y) _ = (x == minX) || (x == maxX) || (y == minY) || (y == maxY)
finite excluded regions = M.filter (\r -> r `notElem` excludedTied) regions
where excludedTied = (0:excluded)
-
regionSizes :: Region -> [(Int, Int)]
regionSizes regions = map (\g -> (g!!0, length g)) $ group $ sort $ M.elems regions
-
largestRegion :: [(Int, Int)] -> Int
largestRegion = maximum . map snd
findBounds :: [Coord] -> (Integer, Integer, Integer, Integer)
-findBounds coords = ( minX - (maxY - minY) -- small x edge
- , maxX + (maxY - minY) -- large x edge
- , minY - (maxX - minX) -- small x edge
- , maxY + (maxX - minX) -- large y edge
+findBounds coords = ( minX -- small x edge
+ , maxX -- large x edge
+ , minY -- small x edge
+ , maxY -- large y edge
)
where maxX = maximum $ map fst coords
minX = minimum $ map fst coords
sc :: Parser ()
sc = L.space (skipSome spaceChar) CA.empty CA.empty
--- sc = L.space (skipSome (char ' ')) CA.empty CA.empty
lexeme = L.lexeme sc
integer = lexeme L.decimal
commaP = symb ","
-
coordFileP = many coordP
coordP = (,) <$> integer <* commaP <*> integer