Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 16, 2024
1 parent fc1c330 commit bf84851
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
3 changes: 2 additions & 1 deletion hs/src/Day14.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Day14 (part1, part1', part2) where

import Common (groupConsecutiveBy)
import Control.Monad (join, liftM2)
import Control.Parallel.Strategies (parList, rdeepseq, withStrategy)
import Data.Char (intToDigit)
import Data.Map qualified as Map (findWithDefault)
import Data.Map.Strict qualified as Map (fromListWith)
Expand Down Expand Up @@ -47,7 +48,7 @@ part2 :: Text -> Either (ParseErrorBundle Text Void) Int
part2 input = do
robots <- parse parser "" input
let (_, bestTime) =
minimum
minimum . withStrategy (parList rdeepseq) $
[ (Down $ maximum $ map length verticalLines, t)
| t <- [0 .. lcm width height - 1],
let verticalLines =
Expand Down
37 changes: 18 additions & 19 deletions hs/src/Day15.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE OverloadedStrings #-}

-- |
Expand Down Expand Up @@ -49,31 +48,31 @@ part1 input = do
move' mover pos' delta =
assert (grid ! pos == '@') . maybe state ((,pos') . (grid //) . sort) $
mover delta pos' [(pos', '@'), (pos, '.')]
moveY, moveX :: Int -> (Int, Int) -> [((Int, Int), Char)] -> Maybe [((Int, Int), Char)]
moveY dy pos'@(y, x) k = case grid ! pos' of
'.' -> Just k
'O' -> moveY dy (y + dy, x) $ ((y + dy, x), 'O') : (pos', '.') : k
'[' ->
assert (grid ! (y, x + 1) == ']') do
k' <-
moveY dy (y + dy, x) $
((y + dy, x), '[') : ((y + dy, x + 1), ']') : ((y, x), '.') : ((y, x + 1), '.') : k
moveY dy (y + dy, x + 1) k'
']' -> assert (grid ! (y, x - 1) == '[') do
k' <- moveY dy (y + dy, x) $ ((y + dy, x - 1), '[') : ((y + dy, x), ']') : ((y, x - 1), '.') : ((y, x), '.') : k
'[' -> assert (grid ! (y, x + 1) == ']') $ do
k' <-
moveY dy (y + dy, x) $
((y + dy, x), '[') : ((y + dy, x + 1), ']') : ((y, x), '.') : ((y, x + 1), '.') : k
moveY dy (y + dy, x + 1) k'
']' -> assert (grid ! (y, x - 1) == '[') $ do
k' <-
moveY dy (y + dy, x) $
((y + dy, x - 1), '[') : ((y + dy, x), ']') : ((y, x - 1), '.') : ((y, x), '.') : k
moveY dy (y + dy, x - 1) k'
_ -> Nothing
moveX dx pos'@(y, x) k = case grid ! pos' of
'.' -> Just k
d | d == 'O' || d == '[' || d == ']' -> moveX dx (y, x + dx) $ ((y, x + dx), d) : (pos', '.') : k
_ -> Nothing
'#' -> Nothing
d -> moveX dx (y, x + dx) $ ((y, x + dx), d) : (pos', '.') : k
move state _ = state

part2 :: Text -> Either String Int
part2 =
part1 . T.concatMap \case
'#' -> "##"
'.' -> ".."
'@' -> "@."
'O' -> "[]"
c -> T.singleton c
part2 = part1 . T.concatMap f
where
f '#' = "##"
f '.' = ".."
f '@' = "@."
f 'O' = "[]"
f c = T.singleton c

0 comments on commit bf84851

Please sign in to comment.