[ (p0,p1,p2,p3) | p0@(x0,y0) <- trees
, p1@(x1,y1) <- trees, p1 > p0
, let dx = x1-x0, let dy = y1-y0
, p2@(x2,y2) <- [ (x0+dy, y0-dx), (x0-dy, y0+dx) ]
, p2 > p0, p2 `elem` trees
, let p3 = (x1 - (x0-x2), y1 - (y0-y2)), p3 `elem` trees ]
Took about three minutes to run in GHCi. But my rule for AoC is that if brute force is simple to implement, I'll do so and let it run while I write a more efficient solution. This one completed before I had to improve it!
EDIT: Now that I think about it, if trees were a Set and not a list, this'd probably run instantly. Maybe this is how you're supposed to do it?
module Main where
import Data.Set qualified as S
parse xs = [(fst x, r) | (r, cs) <- zip [0 ..] (lines xs), x <- zip [0 ..] cs, snd x == '#']
solve' :: String -> Int
solve' xs =
length [() | (x, y) <- S.toList ys, o' <- [1 .. n], o <- [0 .. n], all (`S.member` ys) [(x + o, y + o'), (x + o - o', y + o' + o), (x - o', y + o)]]
where
ys = S.fromList $ parse xs
l = length $ lines xs
h = length $ head $ lines xs
n = min l h - 1
main :: IO ()
main = do
input <- readFile "../inputs/day3.txt"
print $ solve' input
Using a set of (r, c) locations is the way to go for today's puzzle. You can also use a list comprehension in your solve' function to greatly reduce the number of locations to check: for each ordered pair, filter so that the second point is a valid diagonal corner for a possible square, then see if the opposite 2 diagonal corners are members of the set: (code is written in Admiran, which is close enough to Haskell that you should be able to read it -- V2 is a 2-element int and sub is an element-wise subtract on a V2):
countSquares :: grid -> int
countSquares g
= count isSquare corners
where
tls = s.toList g
corners = [(c1, c2) | (c1 : cs) <- tails tls; c2 <- cs; sub c2 c1 |> \(V2 dr dc) -> dr > 0 & dr == dc]
isSquare (V2 r1 c1, V2 r2 c2) = s.member cmploc (V2 r1 c2) g & s.member cmploc (V2 r2 c1) g
Mine runs pretty quick, well the second part not so much it takes like 3 seconds
import Data.Array (Array, (!))
import qualified Data.Array as Array
type Grid = Array (Int, Int) Char
toGrid :: String -> Grid
toGrid s = Array.listArray ((0, 0), (x, y)) (concat rows)
where
rows = lines s
x = length rows - 1
y = length (head rows) - 1
solve1 :: Grid -> Int
solve1 g =
length
[ ()
| ((x, y), '#') <- Array.assocs g,
z <- [1 .. min x y],
let corners = [(x - z, y), (x, y - z), (x - z, y - z)],
all (\c -> g ! c == '#') corners
]
solve2 :: Grid -> Int
solve2 g =
length
[ ()
| ((x, y), '#') <- Array.assocs g,
a <- [1..x],
b <- [0..y-a],
let corners = [(x - a, y - b), (x - a + b, y - a - b), (x + b, y - a)],
all (\c -> inbound c && g ! c == '#') corners
]
where
(_, (m, n)) = Array.bounds g
inbound (x, y) = x >= 0 && x <= m && y >= 0 && y <= n
main :: IO ()
main = do
grid <- toGrid <$> readFile "input.txt"
print $ solve1 grid
print $ solve2 grid
for part 2. new to haskell. I reallly really often felt like there would be a better function for what i'm doing and I dont need to chain a bunch together. I maybe could have done it so that it didn't have to be recursive and it was all in one foldr but idk. Definitely looking for feedback on this. This is for the second part of the question.
EDIT: it didn't work it broke when i was cleaning it up afterwards, fixed now
import Data.Function (on)
import Data.List
main = do
treeMap <- getContents
print . countRotated . toCoordinates $ treeMap
toCoordinates :: String -> [(Int, Int)]
toCoordinates x = foldr (\(a, as) acc -> map (a,) (elemIndices '#' as) ++ acc) [] $ zip [0 ..] $ lines x
countRotated :: [(Int, Int)] -> Int
countRotated [] = 0
countRotated ((x, y) : xs) =
let filtered = filter (\(a, b) -> a >= x || b > y && (a, b) /= (x, y)) xs
in foldr
( \(x1, y1) acc ->
fromEnum
( let dx = x1 - x
dy = y1 - y
in ((&&) `on` flip elem xs) (x + dy, y - dx) (x1 + dy, y1 - dx)
) + acc
) 0 filtered + countRotated xs
2
u/gilgamec 2d ago edited 2d ago
Not how you're supposed to do it:
Took about three minutes to run in GHCi. But my rule for AoC is that if brute force is simple to implement, I'll do so and let it run while I write a more efficient solution. This one completed before I had to improve it!
EDIT: Now that I think about it, if
treeswere aSetand not a list, this'd probably run instantly. Maybe this is how you're supposed to do it?1
u/wyxx_jellyfish 2d ago
New to haskell, this is how I did it
1
u/Atijohn 2d ago
today I learned that the standard library
intersectis O(n2), actually1
u/Haunting_Departure68 1d ago
Mine runs pretty quick, well the second part not so much it takes like 3 seconds
1
u/a-frog-with-no-name 1d ago edited 1d ago
for part 2. new to haskell. I reallly really often felt like there would be a better function for what i'm doing and I dont need to chain a bunch together. I maybe could have done it so that it didn't have to be recursive and it was all in one foldr but idk. Definitely looking for feedback on this. This is for the second part of the question.
EDIT: it didn't work it broke when i was cleaning it up afterwards, fixed now