r/haskell • • 2d ago

AoC Advent of Code 2026 Day 3

https://adventofcode.com/2026/day/3
6 Upvotes

6 comments sorted by

2

u/gilgamec 2d ago edited 2d ago

Not how you're supposed to do it:

[ (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?

1

u/wyxx_jellyfish 2d ago

New to haskell, this is how I did 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

1

u/AustinVelonaut 2d ago

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

1

u/Atijohn 2d ago

today I learned that the standard library intersect is O(n2), actually

import Control.Applicative
import Control.Monad
import Data.Bifunctor
import Data.Function
import Data.List hiding(intersect)

main = print
     . join bimap ((`div` 4) . length)
     . (flip (,) <*> filter (uncurry ((||) `on` (==0)) . snd))
     . head
     . drop 3
     . iterate (intersect <*> sort . map (\((x, y), (dx, dy)) -> ((x + dx, y + dy), (-dy, dx))))
     . map (\((x, y), (x', y')) -> ((x, y), (x' - x, y' - y)))
     . filter (uncurry (/=))
     . join (liftA2 (,))
     . concat
     . zipWith (map . (,)) [0..]
     . map (map fst . filter ((=='#') . snd) . zip [0..])
     . lines
   =<< getContents
  where intersect [] _ = []
        intersect _ [] = []
        intersect (x:xs) (y:ys)
          | x < y = intersect xs (y:ys)
          | x > y = intersect (x:xs) ys
          | otherwise = x : intersect xs ys

1

u/Haunting_Departure68 1d ago

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

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

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