aboutsummaryrefslogtreecommitdiff
path: root/2023/15-Lens_Library/first.hs
diff options
context:
space:
mode:
Diffstat (limited to '2023/15-Lens_Library/first.hs')
-rw-r--r--2023/15-Lens_Library/first.hs38
1 files changed, 38 insertions, 0 deletions
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