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

Print the final position for reference #98

Merged
1 commit merged into from
Dec 16, 2024
Merged
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
36 changes: 26 additions & 10 deletions hs/src/Day14.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

import Common (groupConsecutiveBy)
import Control.Monad (join, liftM2)
import Data.Char (intToDigit)
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 Debug.Trace (traceM)
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)
Expand All @@ -22,7 +24,7 @@
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 27 in hs/src/Day14.hs

View workflow job for this annotation

GitHub Actions / lint

Suggestion in parser in module Day14: Redundant bracket ▫︎ Found: "(,) <$> (L.signed (pure ()) L.decimal <* char ',')\n <*> (L.signed (pure ()) L.decimal)" ▫︎ Perhaps: "(,) <$> (L.signed (pure ()) L.decimal <* char ',')\n <*> L.signed (pure ()) L.decimal"

part1 :: Text -> Either (ParseErrorBundle Text Void) Int
part1 = part1' 101 103
Expand All @@ -44,16 +46,30 @@
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
]
let (_, bestTime) =
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
]
positions =
Map.fromListWith (+) $
[ (((x0 + vx * bestTime) `mod` width, (y0 + vy * bestTime) `mod` height), 1)
| ((x0, y0), (vx, vy)) <- robots
]
line y =
[ case Map.findWithDefault 0 (x, y) positions of
0 -> '.'
n -> if n < 10 then intToDigit n else '+'
| x <- [0 .. width - 1]
]
mapM_ (traceM . line) [0 .. height - 1]
pure bestTime
where
width = 101
height = 103
Loading