Skip to content

Commit

Permalink
Merge pull request #10 from ephemient/hs/day2
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 2, 2024
2 parents ce888c8 + de0a263 commit 06e30b0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Development occurs in language-specific directories:
|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg)|[Python](py) ![Python CI](https://github.com/ephemient/aoc2024/workflows/Python%20CI/badge.svg)|[Rust](rs) ![Rust CI](https://github.com/ephemient/aoc2024/workflows/Rust%20CI/badge.svg)|
|--:|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)||||
2 changes: 2 additions & 0 deletions hs/aoc2024.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ library
hs-source-dirs: src
exposed-modules:
Day1
Day2

other-modules:
Common
Expand Down Expand Up @@ -57,6 +58,7 @@ test-suite aoc2024-test
main-is: Main.hs
other-modules:
Day1Spec
Day2Spec

build-depends:
aoc2024,
Expand Down
2 changes: 2 additions & 0 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text.IO qualified as TIO (readFile)
import Day1 qualified (part1, part2)
import Day2 qualified (part1, part2)
import System.Environment (getArgs, lookupEnv)
import System.FilePath (combine)

Expand All @@ -31,3 +32,4 @@ run' day name showIO funcs = do
main :: IO ()
main = do
run 1 (either fail print) [Day1.part1, Day1.part2]
run 2 (either fail print) [Day2.part1, Day2.part2]
7 changes: 7 additions & 0 deletions hs/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text.IO qualified as TIO (readFile)
import Day1 qualified (part1, part2)
import Day2 qualified (part1, part2)
import System.Environment.Blank (getEnv, setEnv, unsetEnv)
import System.FilePath (combine)

Expand All @@ -29,5 +30,11 @@ main =
"Day 1"
[ bench "part 1" $ nf Day1.part1 input,
bench "part 2" $ nf Day1.part2 input
],
env (getDayInput 2) $ \input ->
bgroup
"Day 2"
[ bench "part 1" $ nf Day2.part1 input,
bench "part 2" $ nf Day2.part2 input
]
]
26 changes: 26 additions & 0 deletions hs/src/Day2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- |
-- Module: Day2
-- Description: <https://adventofcode.com/2024/day/2 Day 2: Red-Nosed Reports>
module Day2 (part1, part2) where

import Common (readEntire)
import Data.Ix (inRange)
import Data.List (inits, tails)
import Data.Text (Text)
import Data.Text qualified as T (lines, words)
import Data.Text.Read qualified as T (decimal)

parse :: Text -> Either String [[Int]]
parse = mapM (mapM (readEntire T.decimal) . T.words) . T.lines

isSafe, isSafe' :: [Int] -> Bool
isSafe report = all (inRange (-3, -1)) delta || all (inRange (1, 3)) delta
where
delta = zipWith (-) report $ drop 1 report
isSafe' report = any isSafe [a ++ b | (a, _ : b) <- zip (inits report) (tails report)]

part1 :: Text -> Either String Int
part1 input = length . filter isSafe <$> parse input

part2 :: Text -> Either String Int
part2 input = length . filter isSafe' <$> parse input
28 changes: 28 additions & 0 deletions hs/test/Day2Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{-# LANGUAGE OverloadedStrings #-}

module Day2Spec (spec) where

import Data.Text (Text)
import Data.Text qualified as T (unlines)
import Day2 (part1, part2)
import Test.Hspec (Spec, describe, it, shouldBe)

example :: Text
example =
T.unlines
[ "7 6 4 2 1",
"1 2 7 8 9",
"9 7 6 2 1",
"1 3 2 4 5",
"8 6 4 4 1",
"1 3 6 7 9"
]

spec :: Spec
spec = do
describe "part 1" $ do
it "examples" $ do
part1 example `shouldBe` Right 2
describe "part 2" $ do
it "examples" $ do
part2 example `shouldBe` Right 4

0 comments on commit 06e30b0

Please sign in to comment.