Day eight
This commit is contained in:
28
day-08-haskell/src/Task1.hs
Normal file
28
day-08-haskell/src/Task1.hs
Normal file
@@ -0,0 +1,28 @@
|
||||
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)
|
||||
|
||||
task1 :: String -> Int
|
||||
task1 input = case readWorld input of
|
||||
((width, height), antennas) -> length $ Set.fromList $ filter isInWorld $ join $ map antinodesOfFrequency $ Map.elems $ multiMapFromList antennas
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user