Safe HaskellNone

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

Documentation

type Address = Int #

type Value = Int #

data Operation #

The operations.

Constructors

Mask Int Int String 
Write Int Int 

Instances

Instances details
Eq Operation # 
Instance details

Defined in Day14

Methods

(==) :: Operation -> Operation -> Bool

(/=) :: Operation -> Operation -> Bool

Show Operation # 
Instance details

Defined in Day14

Methods

showsPrec :: Int -> Operation -> ShowS

show :: Operation -> String

showList :: [Operation] -> ShowS

data Program #

The current program state.

Constructors

Program 

Fields

bin2dec :: String -> Int #

Returns the int for the binary string

sliceTo :: forall a. Int -> Int -> [a] -> [a] #

Take a slice out of a/the list.

input :: String -> [Operation] #

Read the input file.

execute :: Program -> Operation -> Program #

Execute the operation and return the next program state (part1).

part1 :: [Operation] -> Int #

Solve part1.

dec2bin :: Int -> String #

Return the binary string for the given int.

prepend :: Char -> Int -> String -> String #

Prepend the given char before the string.

applyMask :: String -> String -> String #

Apply the mask to the given binary string.

buildMasks :: String -> [String] #

Build all possible masks from mask template.

execute' :: Program -> Operation -> Program #

Execute the operation and return the next program state (part2).

part2 :: [Operation] -> Int #

Solve part2.