This commit is contained in:
2023-12-15 12:21:40 +01:00
parent 8614db903e
commit 8e54e42899
5 changed files with 88 additions and 0 deletions

25
day15/Commons.hs Normal file
View File

@@ -0,0 +1,25 @@
module Commons where
import GHC.IO.Handle (isEOF)
import Data.List.Utils (split)
import Data.Char (ord, digitToInt)
data IType = Set | Remove deriving (Eq, Show)
data Instruction = Instruction { raw :: String, label :: String, focal :: Int, iType :: IType } deriving Show
parseInstructions :: [String] -> [Instruction]
parseInstructions = map (\h -> let label = if last h == '-' then init h else init $ init h
focal = if last h == '-' then 0 else digitToInt $ last h
iType = if last h == '-' then Remove else Set
in Instruction {raw = h, label = label, focal = focal, iType = iType})
parse :: IO [Instruction]
parse = do line <- getLine
let splittedLine = split "," line
return $ parseInstructions splittedLine
getHash :: Int -> String -> Int
getHash = foldl (\ value h -> (17 * (value + ord h)) `mod` 256)