Last active
December 8, 2024 07:08
-
-
Save matthewbauer/e4daabc1d52499a0d8b19b6f091ff3d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Day8 where | |
import Data.Map.Strict qualified as Map | |
import Data.List | |
import Data.Containers.ListUtils (nubOrd) | |
main :: IO () | |
main = do | |
input <- readFile "day8.txt" | |
let (width, height) = (length (head (lines input)), length (lines input)) | |
let antennas = groupBy (\(_, a) (_, b) -> a == b) $ sortBy (\(_, a) (_, b) -> compare a b) $ filter ((/= '.') . snd) [((j :: Int, i :: Int), c) | (line, i) <- zip (lines input) [0..], (c, j) <- zip line [0..]] | |
let maxN = max width height | |
let isValid (x, y) = x >= 0 && x < width && y >= 0 && y < height | |
let getAntinodes (x1, y1) (x2, y2) = filter isValid [(x2 - n * (x1 - x2), y2 - n * (y1 - y2)) | n <- [-maxN..maxN]] | |
let antinodes = nubOrd $ concat $ fmap (\xs -> concat [(getAntinodes a b) | a <- xs, b <- xs, a /= b]) $ fmap (fmap fst) antennas | |
print $ length antinodes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment