blob: a7b9fb87782d184d3aeb6266ac91692be080a142 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
main :: IO ()
main = do
input <- lines <$> getContents
let h = length input
let w = length (head input)
let inBounds (x, y) = 0 <= x && x < w && 0 <= y && y < h
let indexed2 = zipWith (\y -> zipWith (\x -> ((x,y),)) [0::Int ..]) [0::Int ..]
let pts = Map.fromListWith (++) . map (\(p, c) -> (c, [p])) . filter ((/= '.') . snd) $ concat (indexed2 input)
let groups = map snd (Map.assocs pts)
let (x,y) .+ (dx,dy) = (x+dx, y+dy)
let (x,y) .- (dx,dy) = (x-dx, y-dy)
let antinodes1 p q = let d = q .- p in filter inBounds [p .- d, q .+ d]
let antinodes2 p q = let d = q .- p
in takeWhile inBounds (iterate (.- d) p) ++ takeWhile inBounds (iterate (.+ d) q)
let pairs [] = []
pairs (x:xs) = map (x,) xs ++ pairs xs
print $ Set.size $ Set.fromList $ concatMap (concatMap (uncurry antinodes1) . pairs) groups
print $ Set.size $ Set.fromList $ concatMap (concatMap (uncurry antinodes2) . pairs) groups
|