AoC/2021/day05/day05.hs

32 lines
963 B
Haskell
Raw Permalink Normal View History

2021-12-12 01:14:47 +01:00
import Control.Arrow
import Control.Monad
2021-12-12 01:55:00 +01:00
import Data.List.Split
import qualified Data.Map as M
2021-12-12 00:49:19 +01:00
main :: IO ()
main = do
2021-12-12 01:55:00 +01:00
input <-
map (map read . filter (/= "->")) <$> map (splitOneOf ", ") <$> lines <$>
readFile "input"
2021-12-12 01:14:47 +01:00
let process = length . M.filter (> 1) . M.fromListWith (+) . toMap
2021-12-12 01:55:00 +01:00
print $ join (***) process $ (filterOrth input, input)
2021-12-12 00:49:19 +01:00
filterOrth :: [[Int]] -> [[Int]]
filterOrth [] = []
2021-12-12 01:55:00 +01:00
filterOrth ([x1, y1, x2, y2]:ss)
| x1 == x2 || y1 == y2 = [x1, y1, x2, y2] : filterOrth ss
2021-12-12 00:49:19 +01:00
| otherwise = filterOrth ss
2021-12-12 01:55:00 +01:00
toMap :: [[Int]] -> [((Int, Int), Int)]
2021-12-12 00:49:19 +01:00
toMap [] = []
2021-12-12 01:55:00 +01:00
toMap ([x1, y1, x2, y2]:xs)
| y1 == y2 = zip ySame (repeat 1) ++ toMap xs
| x1 == x2 = zip xSame (repeat 1) ++ toMap xs
2021-12-12 00:49:19 +01:00
| otherwise = zip diag (repeat 1) ++ toMap xs
2021-12-12 01:55:00 +01:00
where
diag = zip xRange yRange
ySame = zip xRange (repeat y1)
xSame = zip (repeat x1) yRange
xRange = [x1,x1 - (signum $ x1 - x2) .. x2]
yRange = [y1,y1 - (signum $ y1 - y2) .. y2]