Safe Haskell | None |
---|
Day14
Description
General - The crux of this problem is (obviously) to figure out the masking.
Key is to realize that the given mask is actually two masks: One to set bits and one to unset bits.
You can then mask the given value with ...
band setMask (bor value unsetMask)
Hamler supports bitwise masking on Integers, but (AFAI can see it) has only limited support to work with BitStrings (hence the additional code below to turn a BitString into an Integer).
Part 1 - Just run/fold the operations over the program and when you are done, sum up the memory values.
Part 2 - Initially this sounded/looked easy: Do the same thing again, but with a different implementation for the execution of the operations.
But then I as not able to find a way to implement part2 with a bitwise mask and had to go back to string manipulation to implement the first masking operation (applyMask).
Not elegant, but it works.
Synopsis
- type Address = Int
- type Value = Int
- data Operation
- data Program = Program {}
- bin2dec :: String -> Int
- sliceTo :: forall a. Int -> Int -> [a] -> [a]
- input :: String -> [Operation]
- execute :: Program -> Operation -> Program
- part1 :: [Operation] -> Int
- dec2bin :: Int -> String
- prepend :: Char -> Int -> String -> String
- applyMask :: String -> String -> String
- buildMasks :: String -> [String]
- execute' :: Program -> Operation -> Program
- part2 :: [Operation] -> Int
Documentation
The operations.
The current program state.
execute :: Program -> Operation -> Program #
Execute the operation and return the next program state (part1).
buildMasks :: String -> [String] #
Build all possible masks from mask template.