-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from ephemient/hs/day14
Day 14: Restroom Redoubt
- Loading branch information
Showing
7 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day14 | ||
-- Description: <https://adventofcode.com/2024/day/14 Day 14: Restroom Redoubt> | ||
module Day14 (part1, part1', part2) where | ||
|
||
import Common (groupConsecutiveBy) | ||
import Control.Monad (join, liftM2) | ||
import Data.Map qualified as Map (findWithDefault) | ||
import Data.Map.Strict qualified as Map (fromListWith) | ||
import Data.Ord (Down (Down)) | ||
import Data.Set qualified as Set (fromList, toList) | ||
import Data.String (IsString) | ||
import Data.Text (Text) | ||
import Data.Void (Void) | ||
import Text.Megaparsec (MonadParsec, ParseErrorBundle, Stream (Token, Tokens), parse, sepEndBy1) | ||
import Text.Megaparsec.Char (char, newline, string) | ||
import Text.Megaparsec.Char.Lexer qualified as L (decimal, signed) | ||
|
||
parser :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m [((a, a), (a, a))] | ||
parser = line `sepEndBy1` newline | ||
where | ||
line = (,) <$> (string "p=" *> v2) <*> (string " v=" *> v2) | ||
v2 = (,) <$> (L.signed (pure ()) L.decimal <* char ',') <*> (L.signed (pure ()) L.decimal) | ||
Check warning on line 25 in hs/src/Day14.hs GitHub Actions / lint
|
||
|
||
part1 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part1 = part1' 101 103 | ||
|
||
part1' :: Int -> Int -> Text -> Either (ParseErrorBundle Text Void) Int | ||
part1' width height input = do | ||
robots <- parse parser "" input | ||
let totals = | ||
Map.fromListWith (+) $ | ||
[ ((compare x $ width `div` 2, compare y $ height `div` 2), 1) | ||
| ((x0, y0), (vx, vy)) <- robots, | ||
let x = (x0 + vx * t) `mod` width | ||
y = (y0 + vy * t) `mod` height | ||
] | ||
pure $ product [Map.findWithDefault 0 k totals | k <- join (liftM2 (,)) [LT, GT]] | ||
where | ||
t = 100 | ||
|
||
part2 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part2 input = do | ||
robots <- parse parser "" input | ||
pure . snd . minimum $ | ||
[ (Down $ maximum $ map length verticalLines, t) | ||
| t <- [0 .. lcm width height - 1], | ||
let verticalLines = | ||
groupConsecutiveBy isLine . Set.toList . Set.fromList $ | ||
[ ((y0 + vy * t) `mod` height, (x0 + vx * t) `mod` width) | ||
| ((x0, y0), (vx, vy)) <- robots | ||
] | ||
isLine (y0, x0) (y1, x1) = y0 == y1 && x0 + 1 == x1 | ||
] | ||
where | ||
width = 101 | ||
height = 103 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day14Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day14 (part1') | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "p=0,4 v=3,-3", | ||
"p=6,3 v=-1,-3", | ||
"p=10,3 v=-1,2", | ||
"p=2,0 v=2,-1", | ||
"p=0,0 v=1,3", | ||
"p=3,0 v=-2,-2", | ||
"p=7,6 v=-1,-3", | ||
"p=3,0 v=-1,-2", | ||
"p=9,3 v=2,3", | ||
"p=7,3 v=-1,2", | ||
"p=2,4 v=2,-3", | ||
"p=9,5 v=-3,-3" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1' 11 7 example `shouldBe` Right 12 |