Skip to content

Commit

Permalink
Merge pull request #220 from ephemient/hs/day20
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Jan 7, 2025
2 parents b3336ed + 1780fcc commit b15f043
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion hs/src/Day11.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ solve n input = do
let ix = num `mod` sizeofMutableArray array
readArray array ix >>= writeArray array ix . IntMap.insertWith (+) num 1
end = iterate step start !! n
pure $ foldl' (flip $ (+) . foldl' (+) 0) 0 end
pure $ foldl' (foldl' (+)) 0 end

step :: Array (IntMap Int) -> Array (IntMap Int)
step array = createArray (sizeofArray array) IntMap.empty $ \array' -> do
Expand Down
18 changes: 10 additions & 8 deletions hs/src/Day20.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
module Day20 (solve) where

import Control.Parallel.Strategies (parMap, rseq)
import Data.List (tails)
import Data.Map qualified as Map (empty, member, size, toList)
import Data.Map.Strict qualified as Map (insert)
import Data.List (sort, tails)
import Data.Maybe (listToMaybe)
import Data.Text (Text)
import Data.Text qualified as T (index, length, lines, unpack)
import Data.Vector qualified as V (fromList, length, (!))
Expand All @@ -21,17 +20,20 @@ solve cheats time input =
]
| y0 <- [0 .. V.length grid - 1],
(x0, 'S') <- zip [0 ..] . T.unpack $ grid V.! y0,
((y1, x1), i) : rest <- paths Map.empty (y0, x0) >>= tails . Map.toList
((y1, x1), i) : rest <- take 1 (paths 0 [] (y0, x0)) >>= tails . sort
]
where
grid = V.fromList $ T.lines input
paths path pos@(y, x)
paths n path pos@(y, x)
| y < 0 || V.length grid <= y = []
| x < 0 || T.length line <= x = []
| '#' <- line `T.index` x = []
| 'E' <- line `T.index` x = [path']
| pos `Map.member` path = []
| otherwise = concatMap (paths path') [(y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)]
| otherwise =
filter
(maybe (const True) ((/=) . fst) $ listToMaybe path)
[(y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)]
>>= paths (n + 1) path'
where
line = grid V.! y
path' = Map.insert pos (Map.size path) path
path' = (pos, n) : path

0 comments on commit b15f043

Please sign in to comment.