From 7aaca2a218947b476b3e7368b22b94f3cc4d67ed Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 13 Jan 2024 00:46:30 +0100 Subject: 2023-15 in haskell --- 2023/15-Lens_Library/first.hs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2023/15-Lens_Library/first.hs (limited to '2023/15-Lens_Library/first.hs') diff --git a/2023/15-Lens_Library/first.hs b/2023/15-Lens_Library/first.hs new file mode 100644 index 0000000..86cca87 --- /dev/null +++ b/2023/15-Lens_Library/first.hs @@ -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 -- cgit v1.2.3