Files
advent-of-code-2023/day15/Commons.hs
2023-12-15 12:24:33 +01:00

25 lines
963 B
Haskell

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 :: String -> Int -> Int
getHash [] value = value
getHash (h: t) value = getHash t (17 * (value + ord h)) `mod` 256