aboutsummaryrefslogtreecommitdiff
path: root/2020/14-Docking_Data/second.hs
blob: d1b6f3009cc6e9336a90a683e8d08379f529af80 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-- requires cabal install --lib megaparsec parser-combinators
module Main (main) where
import Control.Monad (void, when)
import Data.Bits (clearBit, setBit)
import Data.List (foldl')
import Data.Map qualified as M
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)

exampleExpectedOutput = 208

type Bits = [Maybe Bool]
data Op = Mask Bits| Mem Int Int
type Input = [Op]

type Parser = Parsec Void String

parseMask :: Parser Op
parseMask = do
  void $ string "mask = "
  bits <- some $ (char '1' *> (return $ Just True)) <|> (char '0' *> (return $ Just False)) <|> (char 'X' *> (return Nothing))
  return $ Mask bits

parseMem :: Parser Op
parseMem = do
  void $ string "mem["
  index <- some digitChar
  void $ string "] = "
  value <- some digitChar
  return $ Mem (read index) (read value)

parseOps :: Parser Input
parseOps = do
  ops <- some $ (parseMask <|> parseMem) <* (optional $ char '\n')
  void $ eof
  return $ ops

parseInput :: String -> IO Input
parseInput filename = do
  input <- readFile filename
  case runParser parseOps filename input of
    Left bundle -> die $ errorBundlePretty bundle
    Right ops -> return ops

computeOp :: Int -> Bits -> [Int]
computeOp index bits = fst $ foldl' evalBit ([index], 35) bits
  where
    evalBit :: ([Int], Int) -> Maybe Bool -> ([Int], Int)
    evalBit (acc, bit) (Just False) = (acc, bit-1)
    evalBit (acc, bit) (Just True) = (map (\x -> setBit x bit) acc, bit-1)
    evalBit (acc, bit) Nothing = ((map (\x -> setBit x bit) acc) ++ (map (\x -> clearBit x bit) acc), bit-1)

compute :: Input -> Int
compute = extract . foldl' compute init
  where
    extract :: (M.Map Int Int, Bits) -> Int
    extract = sum . M.elems . fst
    compute :: (M.Map Int Int, Bits) -> Op -> (M.Map Int Int, Bits)
    compute (acc, _) (Mask bits) = (acc, bits)
    compute (acc, bits) (Mem index value) = (newAcc, bits)
      where
        newAcc :: M.Map Int Int
        newAcc = foldl' eval acc (computeOp index bits)
        eval :: M.Map Int Int -> Int -> M.Map Int Int
        eval acc' index' = M.insert index' value acc'
    init :: (M.Map Int Int, Bits)
    init = (M.empty, [])

main :: IO ()
main = do
  example <- parseInput "example2"
  let exampleOutput = compute example
  when  (exampleOutput /= exampleExpectedOutput)  (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
  input <- parseInput "input"
  print $ compute input