-
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 #10 from ephemient/hs/day2
- Loading branch information
Showing
6 changed files
with
66 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,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 |
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,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 |