-
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 #57 from ephemient/hs/day7
- Loading branch information
Showing
6 changed files
with
79 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,36 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day7 | ||
-- Description: <https://adventofcode.com/2024/day/7 Day 7: Bridge Repair> | ||
module Day7 (part1, part2) where | ||
|
||
import Common (readEntire, readSome) | ||
import Control.Monad (ap) | ||
import Control.Parallel.Strategies (parMap, rseq) | ||
import Data.List.NonEmpty (NonEmpty ((:|))) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (lines, stripPrefix) | ||
import Data.Text.Read (Reader) | ||
import Data.Text.Read qualified as T (decimal) | ||
|
||
parseLine :: (Integral a) => Reader (a, NonEmpty a) | ||
parseLine line = do | ||
(lhs, line') <- T.decimal line | ||
(rhs, line'') <- maybe (Left "") (readSome T.decimal) $ T.stripPrefix ": " line' | ||
pure ((lhs, rhs), line'') | ||
|
||
parFilter :: (a -> Bool) -> [a] -> [a] | ||
parFilter f = fmap fst . filter snd . (zip `ap` parMap rseq f) | ||
|
||
solve :: (Integral a) => (a -> a -> [a]) -> Text -> Either String a | ||
solve op input = sum . map fst . parFilter f <$> mapM (readEntire parseLine) (T.lines input) | ||
where | ||
f (lhs, r0 :| []) = lhs == r0 | ||
f (lhs, r0 :| r1 : rhs) = or [r2 <= lhs && f (lhs, r2 :| rhs) | r2 <- r0 `op` r1] | ||
|
||
part1 :: Text -> Either String Int | ||
part1 = solve $ \a b -> [a + b, a * b] | ||
|
||
part2 :: Text -> Either String Int | ||
part2 = solve $ \a b -> [a + b, a * b, read $ show a ++ show b] |
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 Day7Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day7 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "190: 10 19", | ||
"3267: 81 40 27", | ||
"83: 17 5", | ||
"156: 15 6", | ||
"7290: 6 8 6 15", | ||
"161011: 16 10 13", | ||
"192: 17 8 14", | ||
"21037: 9 7 18 13", | ||
"292: 11 6 16 20" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` Right 3749 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` Right 11387 |