Skip to content

DayTwo refactor + CLI #1

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ramirez7
Copy link

@ramirez7 ramirez7 commented Jun 20, 2023

  • Refactored DayTwo to use an Either String to handle parse errors instead of the N* constructors.
    • I used Bifunctor, Traversable, and Applicative to process this either.
      • first from Bifunctor was used to augment the String error message with context.
      • traverse was used to process all the lines and short-circuit on first error
      • Applicative style (_ <$> _ <*> _) was used to process both sides of the round and pass them to the Round Vs constructor. LYAH has a good section on applicative style
  • Reworked the types
    • Used fewer type synonyms (e.g. I think [Round] is preferable to Rounds)
    • Made Round a proper data type + used an infix Vs constructor. This just looks nice :)
    • Made a type for Win/Loss/Draw
  • Wrote generally more point-free functions
    • I really like LambdaCase and used it quite a bit. It's a good syntax extension to learn!

I also made app/Main.hs an entrypoint for all the various days' mains. It parses the day number as the first argument, and then calls the correct day main with the rest of the args. Here's some example usage:

[armando@nixos:~/work/github/ola-advent-of-code-2022/haskell]$ cabal run haskell-exe -- 2 ./src/DayTwo/input.txt 
Up to date
12855

[armando@nixos:~/work/github/ola-advent-of-code-2022/haskell]$ cabal run haskell-exe -- 2 ./src/DayTwo/badinput.txt
Up to date
haskell-exe: Error in round 20 : Error parsing PlayOption: Prelude.read: no parse
CallStack (from HasCallStack):
  error, called at src/DayTwo/Solution.hs:76:58 in haskell-0.1.0.0-inplace:DayTwo.Solution

[armando@nixos:~/work/github/ola-advent-of-code-2022/haskell]$ cabal run haskell-exe -- 1 ./src/DayOne/input.txt 
Up to date
71934

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant