2023-15 in haskell

This commit is contained in:
Julien Dessaux 2024-01-13 00:46:30 +01:00
parent 1234d4b5bc
commit 7aaca2a218
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1 @@
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7

View file

@ -0,0 +1,38 @@
-- requires cabal install --lib megaparsec parser-combinators split
module Main (main) where
import Control.Applicative.Permutations
import Control.Monad (void, when)
import Data.Char qualified as C
import Data.Either
import Data.Functor
import Data.List qualified as L
import Data.Map qualified as M
import Data.Maybe
import Data.Set qualified as S
import Data.List.Split qualified as DLS
import Data.Vector qualified as V
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import Debug.Trace
exampleExpectedOutput = 1320
compute :: String -> Int
compute input = sum $ map (hash 0) items
where
items :: [String]
items = DLS.endByOneOf ",\n" input
hash :: Int -> String -> Int
hash i [] = i
hash i (x:xs) = hash (((i + C.ord x) * 17) `rem` 256) xs
main :: IO ()
main = do
example <- readFile "example"
let exampleOutput = compute example
when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
input <- readFile "input"
print $ compute input

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,89 @@
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where
import Control.Applicative.Permutations
import Control.Monad (void, when)
import Data.Char qualified as C
import Data.Either
import Data.Functor
import Data.List qualified as L
import Data.Map qualified as M
import Data.Maybe
import Data.Set qualified as S
import Data.Vector qualified as V
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import Debug.Trace
exampleExpectedOutput = 145
data Op = Equal Int | Minus deriving (Eq, Show)
data Step = Step String Op deriving (Eq, Show)
type Input = [Step]
type Parser = Parsec Void String
parseNumber :: Parser Int
parseNumber = read <$> some digitChar <* optional (char ',')
parseOp :: Parser Op
parseOp = char '-' $> Minus
<|> char '=' *> (Equal <$> parseNumber)
parseStep :: Parser Step
parseStep = Step <$> some letterChar
<*> parseOp <* optional (char ',')
parseInput' :: Parser Input
parseInput' = some parseStep <* eol <* eof
parseInput :: String -> IO Input
parseInput filename = do
input <- readFile filename
case runParser parseInput' filename input of
Left bundle -> error $ errorBundlePretty bundle
Right input' -> return input'
data Lens = Lens String Int deriving (Eq, Show)
type Box = [Lens]
compute :: Input -> Int
compute input = sum $ zipWith (curry score) [1..] $ L.foldl' step initialBoxes input
where
score :: (Int, Box) -> Int
score (i, lenses) = sum $ zipWith (curry $ scoreLens i) [1 .. ] lenses
scoreLens :: Int -> (Int, Lens) -> Int
scoreLens i (j, Lens _ k) = i * j * k
initialBoxes = replicate 256 []
step :: [Box] -> Step -> [Box]
step boxes (Step label Minus) = take i boxes
++ remove [] (boxes L.!! i)
: drop (i+1) boxes
where
i = hash 0 label
remove :: Box -> Box -> Box
remove out [] = out
remove out (l@(Lens ll _):ls) | ll == label = remove out ls
| otherwise = remove (out ++ [l]) ls
step boxes (Step label (Equal f)) = take i boxes
++ process [] (boxes L.!! i)
: drop (i+1) boxes
where
i = hash 0 label
process :: Box -> Box -> Box
process out [] = out ++ [Lens label f]
process out (l@(Lens ll _):ls) | ll == label = out ++ Lens label f : ls
| otherwise = process (out ++ [l]) ls
hash :: Int -> String -> Int
hash i [] = i
hash i (x:xs) = hash (((i + C.ord x) * 17) `rem` 256) xs
main :: IO ()
main = do
example <- parseInput "example"
let exampleOutput = compute example
when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
input <- parseInput "input"
print $ compute input