Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor tweaks #101

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading