aboutsummaryrefslogtreecommitdiff
path: root/2023/15-Lens_Library/first.hs
blob: 86cca875cf4d5bce594a12e6c26d7d358421dd44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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