Day eight

This commit is contained in:
2024-12-08 13:40:17 +01:00
parent 8286304531
commit ff74e85ba6
11 changed files with 300 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
module Task2 (task2) where
import Common (Position, readWorld, multiMapFromList, pairPermutations)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Control.Monad (join)
task2 :: String -> Int
task2 input = case readWorld input of
((width, height), antennas) -> length $ Set.fromList $ join $ map antinodesOfFrequency $ Map.elems $ multiMapFromList antennas
where
antinodesOfFrequency :: [Position] -> [Position]
antinodesOfFrequency nodes = pairPermutations nodes >>= antinodesOfPair
antinodesOfPair :: (Position, Position) -> [Position]
antinodesOfPair (a@(ax, ay), b@(bx, by)) =
let diffX = bx - ax
diffY = by - ay
in castInWorld a (-diffX, -diffY) ++ castInWorld b (diffX, diffY)
castInWorld (sx, sy) (ox, oy) =
takeWhile isInWorld $ map (\f -> (sx + ox * f, sy + oy * f)) [0..]
isInWorld :: Position -> Bool
isInWorld (x, y)
| x < 0 = False
| x >= width = False
| y < 0 = False
| y >= height = False
| otherwise = True