summaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-12-09 20:53:15 +0100
committerTom Smeding <tom@tomsmeding.com>2024-12-09 20:53:15 +0100
commit59f1776dd7a967114773ed87b039871920cfae18 (patch)
tree7c77c0fc11d07a2e90464bec33cc84571deafb37 /2024
parenta74271d093a3bcb05c75fa4a5e40ff60665ecdb6 (diff)
8
Diffstat (limited to '2024')
-rw-r--r--2024/8.hs21
1 files changed, 21 insertions, 0 deletions
diff --git a/2024/8.hs b/2024/8.hs
new file mode 100644
index 0000000..a7b9fb8
--- /dev/null
+++ b/2024/8.hs
@@ -0,0 +1,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