-
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 #40 from ephemient/hs/day5
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
-- | | ||
-- Module: Day5 | ||
-- Description: <https://adventofcode.com/2024/day/5 Day 5: Print Queue> | ||
module Day5 (part1, part2) where | ||
|
||
import Control.Arrow (second) | ||
import Data.IntMap qualified as IntMap (findWithDefault, fromList, (!?)) | ||
import Data.IntMap.Strict qualified as IntMap (fromListWith) | ||
import Data.IntSet qualified as IntSet (empty, member, singleton, toList, union) | ||
import Data.List (sortBy) | ||
import Data.Maybe (fromJust, fromMaybe) | ||
import Data.String (IsString) | ||
import Data.Text (Text) | ||
import Data.Void (Void) | ||
import Text.Megaparsec (MonadParsec, ParseErrorBundle, Stream (Token, Tokens), parse, sepEndBy, sepEndBy1, skipMany) | ||
import Text.Megaparsec.Char (char, newline) | ||
import Text.Megaparsec.Char.Lexer (decimal) | ||
|
||
parser :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m ([(a, a)], [[a]]) | ||
parser = | ||
(,) | ||
<$> ((,) <$> decimal <* char '|' <*> decimal) `sepEndBy` newline | ||
<* skipMany newline | ||
<*> (decimal `sepEndBy1` char ',') `sepEndBy` newline | ||
|
||
part1 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part1 input = do | ||
(deps, updates) <- parse parser "" input | ||
pure $ | ||
sum | ||
[ update !! (length update `div` 2) | ||
| update <- updates, | ||
let order = IntMap.fromList @Int $ zip update [0 ..], | ||
and | ||
[ fromMaybe True $ (<) <$> order IntMap.!? a <*> order IntMap.!? b | ||
| (a, b) <- deps | ||
] | ||
] | ||
|
||
part2 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part2 input = do | ||
(deps, updates) <- parse parser "" input | ||
let deps' = IntMap.fromListWith IntSet.union $ second IntSet.singleton <$> deps | ||
tryCompare a b | ||
| a == b = Just EQ | ||
| b `IntSet.member` (IntMap.findWithDefault IntSet.empty a deps') = Just LT | ||
Check warning on line 46 in hs/src/Day5.hs GitHub Actions / lint
|
||
| a `IntSet.member` (IntMap.findWithDefault IntSet.empty b deps') = Just GT | ||
Check warning on line 47 in hs/src/Day5.hs GitHub Actions / lint
|
||
| otherwise = | ||
mconcat (tryCompare a <$> maybe [] IntSet.toList (deps' IntMap.!? b)) | ||
<> fmap (compare EQ) (mconcat (tryCompare b <$> maybe [] IntSet.toList (deps' IntMap.!? a))) | ||
compare' a b = fromJust $ tryCompare a b | ||
pure $ | ||
sum | ||
[ update' !! (length update `div` 2) | ||
| update <- updates, | ||
let update' = sortBy compare' update, | ||
update /= update' | ||
] |
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,50 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day5Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day5 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "47|53", | ||
"97|13", | ||
"97|61", | ||
"97|47", | ||
"75|29", | ||
"61|13", | ||
"75|53", | ||
"29|13", | ||
"97|29", | ||
"53|29", | ||
"61|53", | ||
"97|53", | ||
"61|29", | ||
"47|13", | ||
"75|47", | ||
"97|75", | ||
"47|61", | ||
"75|61", | ||
"47|29", | ||
"75|13", | ||
"53|13", | ||
"", | ||
"75,47,61,53,29", | ||
"97,61,53,29,13", | ||
"75,29,13", | ||
"75,97,47,61,53", | ||
"61,13,29", | ||
"97,13,75,29,47" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` Right 143 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` Right 123 |