-
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.
- Loading branch information
Showing
6 changed files
with
63 additions
and
3 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,29 @@ | ||
{-| | ||
Module: Day6 | ||
Description: <https://adventofcode.com/2023/day/6 Day 6: Wait For It> | ||
-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day6 (part1, part2) where | ||
|
||
import Common (readEntire) | ||
import Data.Char (isDigit) | ||
import Data.Function (on) | ||
import Data.List (unfoldr) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T (breakOn, dropWhile, filter) | ||
import qualified Data.Text.Read as T (decimal) | ||
|
||
winCount :: Int -> Int -> Int | ||
winCount time distance = ceiling (b + d - 1) - floor (b - d + 1) + 1 where | ||
b = fromIntegral time / 2 | ||
d = sqrt $ b * b - fromIntegral distance | ||
|
||
part1 :: Text -> Int | ||
part1 = product . uncurry (zipWith winCount `on` unfoldr reader) . T.breakOn "\n" | ||
where reader = either (const Nothing) Just . T.decimal . T.dropWhile (not . isDigit) | ||
|
||
part2 :: Text -> Either String Int | ||
part2 input = winCount <$> | ||
readEntire T.decimal (T.filter isDigit line1) <*> | ||
readEntire T.decimal (T.filter isDigit line2) | ||
where (line1, line2) = T.breakOn "\n" input |
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,22 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
module Day6Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import qualified Data.Text as T (unlines) | ||
import Day6 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = T.unlines | ||
[ "Time: 7 15 30" | ||
, "Distance: 9 40 200" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example `shouldBe` 288 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example `shouldBe` Right 71503 |