import Data.List main :: IO () main = do input <- readFile "input" let grid = lines input let antennas = getAntennas grid let nodes = filter (isInBounds (gridBounds grid) ) . nub . flatten . map getAntinodes . groupAntennas $ antennas print $ length nodes isInBounds (h,w) (y,x) = y >= 0 && y < h && x >= 0 && x < w getAntinodes coords = let n = length coords node (y1,x1) (y2,x2) = (y2 + y2-y1, x2 + x2-x1) in [node (coords!!j) (coords!!i) | j <- [0..n-1], i <- [0..n-1], i /= j] groupAntennas = map (map snd) . groupBy (\a b -> fst a == fst b) . sortOn fst flatten l = foldl (++) [] l gridBounds grid = (length grid, length (head grid)) getAntennas grid = let (n,m) = gridBounds grid in [(grid!!i!!j, (i,j)) | i <- [0..n-1], j <- [0..m-1], grid!!i!!j /= '.']