Skip to content

Commit

Permalink
Day 6: Wait For It
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 6, 2023
1 parent 6de853c commit 20a78d2
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development occurs in language-specific directories:
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day3.kt)|[day3.py](py/aoc2023/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day4.kt)|[day4.py](py/aoc2023/day4.py)|[day4.rs](rs/src/day4.rs)|
|[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day5.kt)|[day5.py](py/aoc2023/day5.py)|[day5.rs](rs/src/day5.rs)|
||[Day6.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day6.kt)|||
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day6.kt)|||
6 changes: 4 additions & 2 deletions hs/aoc2023.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ library
Day2,
Day3,
Day4,
Day5
Day5,
Day6

-- Modules included in this library but not exported.
other-modules:
Expand Down Expand Up @@ -75,7 +76,8 @@ test-suite aoc2023-test
Day2Spec,
Day3Spec,
Day4Spec,
Day5Spec
Day5Spec,
Day6Spec
build-depends:
aoc2023,
base ^>=4.17.2.0,
Expand Down
2 changes: 2 additions & 0 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import qualified Day2 (part1, part2)
import qualified Day3 (part1, part2)
import qualified Day4 (part1, part2)
import qualified Day5 (part1, part2)
import qualified Day6 (part1, part2)

import Control.Monad (ap, when)
import Data.Foldable (find)
Expand Down Expand Up @@ -40,3 +41,4 @@ main = do
run 3 print [Day3.part1, Day3.part2]
run 4 (either fail print) [Day4.part1, Day4.part2]
run 5 (either (fail . errorBundlePretty) print) [Day5.part1, Day5.part2]
run 6 (either fail print) [pure . Day6.part1, Day6.part2]
5 changes: 5 additions & 0 deletions hs/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import qualified Day2 (part1, part2)
import qualified Day3 (part1, part2)
import qualified Day4 (part1, part2)
import qualified Day5 (part1, part2)
import qualified Day6 (part1, part2)
import System.Environment (lookupEnv)
import System.FilePath (combine)

Expand Down Expand Up @@ -40,4 +41,8 @@ main = defaultMain
[ bench "part 1" $ nf Day5.part1 input
, bench "part 2" $ nf Day5.part2 input
]
, env (getDayInput 6) $ \input -> bgroup "Day 6"
[ bench "part 1" $ nf Day6.part1 input
, bench "part 2" $ nf Day6.part2 input
]
]
29 changes: 29 additions & 0 deletions hs/src/Day6.hs
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
22 changes: 22 additions & 0 deletions hs/test/Day6Spec.hs
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

0 comments on commit 20a78d2

Please sign in to comment.