Files
adventofcode24/day-08-haskell/src/Task1.hs

30 lines
993 B
Haskell

module Task1 (task1) where
import Common (Position, readWorld, multiMapFromList, pairPermutations)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Control.Monad (join)
import Flow
task1 :: String -> Int
task1 input = case readWorld input of
((width, height), antennas) -> multiMapFromList antennas |> Map.elems |> map antinodesOfFrequency |> join |> filter isInWorld |> Set.fromList |> length
where
antinodesOfFrequency :: [Position] -> [Position]
antinodesOfFrequency nodes = pairPermutations nodes >>= antinodesOfPair
antinodesOfPair :: (Position, Position) -> [Position]
antinodesOfPair ((ax, ay), (bx, by)) =
let diffX = bx - ax
diffY = by - ay
in [(ax - diffX, ay - diffY), (bx + diffX, by + diffY)]
isInWorld :: Position -> Bool
isInWorld (x, y)
| x < 0 = False
| x >= width = False
| y < 0 = False
| y >= height = False
| otherwise = True