2023.01.1 in Haskell
This commit is contained in:
parent
c50bcb092e
commit
cb0c8c2845
2 changed files with 44 additions and 0 deletions
|
@ -1 +1,3 @@
|
|||
# [Day 1: Trebuchet?!](https://adventofcode.com/2023/day/1)
|
||||
|
||||
The Haskell implementation included here is only partial, mostly because I ran out of patience with it after taking an age to implement the CLI required.
|
||||
|
|
42
challenges/2023/01-trebuchet/main.hs
Normal file
42
challenges/2023/01-trebuchet/main.hs
Normal file
|
@ -0,0 +1,42 @@
|
|||
import System.Environment
|
||||
import System.Exit
|
||||
import System.IO
|
||||
import Data.Maybe
|
||||
import Data.Char
|
||||
import Control.Exception
|
||||
|
||||
type ChallengeReturn = Int
|
||||
|
||||
parse :: String -> [String]
|
||||
parse = lines
|
||||
|
||||
one :: String -> ChallengeReturn
|
||||
one inp = foldl
|
||||
(\acc line ->
|
||||
let digits = map (\x -> ord x - ord '0') (filter isDigit line) in
|
||||
assert ((length digits) /= 0) (
|
||||
acc + ((digits!!0) * 10) + (digits!!((length digits) - 1))
|
||||
)
|
||||
)
|
||||
0
|
||||
(parse inp)
|
||||
|
||||
two :: String -> ChallengeReturn
|
||||
two inp = undefined
|
||||
|
||||
main :: IO ()
|
||||
main = do args <- getArgs
|
||||
inp <- getContents
|
||||
_runFn (_selectFn args) inp
|
||||
|
||||
_selectFn :: [String] -> Maybe (String -> ChallengeReturn)
|
||||
_selectFn ["1"] = Just one
|
||||
_selectFn ["2"] = Just two
|
||||
_selectFn _ = Nothing
|
||||
|
||||
_runFn :: Maybe (String -> ChallengeReturn) -> String -> IO ()
|
||||
_runFn Nothing _ = _debug "Missing or invalid day argument" >> exitWith (ExitFailure 1)
|
||||
_runFn (Just fn) inp = putStrLn (show (fn inp)) >> exitWith ExitSuccess
|
||||
|
||||
_debug :: String -> IO ()
|
||||
_debug x = do hPutStrLn stderr x
|
Loading…
Add table
Add a link
Reference in a new issue