Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors #8

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion hs/aoc2024.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ executable aoc2024
aoc2024,
base ^>=4.20.0.0,
filepath ^>=1.5.2.0,
megaparsec ^>=9.7.0,
text ^>=2.1.1
ghc-options: -no-hs-main -threaded -Wall
default-language: GHC2024
Expand Down
3 changes: 1 addition & 2 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Data.Function (on)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text.IO as TIO (putStrLn, readFile)
import Text.Megaparsec (errorBundlePretty)
import System.Environment (getArgs, lookupEnv)
import System.FilePath (combine)

Expand All @@ -32,4 +31,4 @@ run' day name showIO funcs = do

main :: IO ()
main = do
run 1 print [Day1.part1, Day1.part2]
run 1 (either fail print) [Day1.part1, Day1.part2]
28 changes: 18 additions & 10 deletions hs/src/Day1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ Description: <https://adventofcode.com/2024/day/1 Day 1: Historian Hysteria>
-}
module Day1 (part1, part2) where

import Common (readEntire)
import Data.Function (on)
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap (fromListWith, findWithDefault)
import Data.List (sort, transpose)
import Data.Text (Text)
import qualified Data.Text as T (lines, words, unpack)
import qualified Data.Text as T (lines, words)
import qualified Data.Text.Read as T (decimal)

part1 :: Text -> Int
part1 input = sum $ abs <$> cs where
[as, bs] = transpose $ map (map (read . T.unpack) . T.words) $ T.lines input
cs = (zipWith (-) `on` sort) as bs
parse :: Text -> Either String [[Int]]
parse = fmap transpose . mapM (mapM (readEntire T.decimal) . T.words) . T.lines

part2 :: Text -> Int
part2 input = sum [a * IntMap.findWithDefault 0 a cs | a <- as] where
[as, bs] = transpose $ map (map (read . T.unpack) . T.words) $ T.lines input
cs = IntMap.fromListWith (+) [(b, 1) | b <- bs]
part1 :: Text -> Either String Int
part1 input = case parse input of
Left err -> Left err
Right [as, bs] -> pure $ sum $ abs <$> (zipWith (-) `on` sort) as bs
_ -> Left "no parse"

part2 :: Text -> Either String Int
part2 input = case parse input of
Left err -> Left err
Right [as, bs] ->
let cs = IntMap.fromListWith (($!) . (+)) [(b, 1) | b <- bs]
in pure $ sum [a * IntMap.findWithDefault 0 a cs | a <- as]
_ -> Left "no parse"
4 changes: 2 additions & 2 deletions hs/test/Day1Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spec :: Spec
spec = do
describe "part 1" $ do
it "examples" $ do
part1 example `shouldBe` 11
part1 example `shouldBe` Right 11
describe "part 2" $ do
it "examples" $ do
part2 example `shouldBe` 31
part2 example `shouldBe` Right 31
Loading