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

26 lines
938 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 :: Int -> String -> Int
getHash = foldl (\ value h -> (17 * (value + ord h)) `mod` 256)