From 76f46123c958f2bb5ca05148c30b41fb9c5a613c Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Tue, 22 Dec 2020 14:22:53 +0000 Subject: [PATCH] Day 14 part 2 --- advent14/src/advent14.hs | 73 +++++++++++---- data/advent14b.txt | 4 + problems/day14.html | 192 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 249 insertions(+), 20 deletions(-) create mode 100644 data/advent14b.txt create mode 100644 problems/day14.html diff --git a/advent14/src/advent14.hs b/advent14/src/advent14.hs index 87aaa3d..d03b33f 100644 --- a/advent14/src/advent14.hs +++ b/advent14/src/advent14.hs @@ -10,64 +10,95 @@ import Control.Applicative import Data.Int import Data.Bits -import Data.Char import Data.List - import qualified Data.Map.Strict as M -import Data.Map.Strict ((!)) -type MaskMap = M.Map Int Int +data MaskValue = Zero | One | Wild deriving (Show, Eq) +type MaskMap = M.Map Int MaskValue data Instruction = Mask MaskMap | Assignment Int64 Int64 deriving (Show, Eq) type Memory = M.Map Int64 Int64 data Machine = Machine { mMemory :: Memory + , mMask :: MaskMap , mMask0 :: Int64 , mMask1 :: Int64 } deriving (Show, Eq) -emtpyMachine = Machine M.empty (complement 0) 0 +emtpyMachine = Machine M.empty M.empty (complement 0) 0 main :: IO () main = do text <- TIO.readFile "data/advent14.txt" let program = successfulParse text - print $ take 6 program print $ part1 program + print $ part2 program part1 program = sum $ M.elems $ mMemory finalMachine - where finalMachine = executeInstructions program + where finalMachine = executeInstructions1 program -executeInstructions instructions = - foldl' executeInstruction emtpyMachine instructions +part2 program = sum $ M.elems $ mMemory finalMachine + where finalMachine = executeInstructions2 program -executeInstruction machine (Mask mask) = makeMask machine mask -executeInstruction machine (Assignment loc value) = assignValue machine loc value +executeInstructions1 instructions = + foldl' executeInstruction1 emtpyMachine instructions +executeInstruction1 machine (Mask mask) = makeMask machine mask +executeInstruction1 machine (Assignment loc value) = + assignValue machine loc value makeMask machine mask = machine {mMask0 = maskZeroes mask, mMask1 = maskOnes mask} -maskValue machine value = - (value .|. (mMask1 machine)) .&. (mMask0 machine) - assignValue machine loc value = machine {mMemory = M.insert loc value' mem} where value' = maskValue machine value mem = mMemory machine +maskValue machine value = + (value .|. (mMask1 machine)) .&. (mMask0 machine) maskOnes :: MaskMap -> Int64 maskOnes mask = foldl' setBit zeroBits ones - where ones = M.keys $ M.filter (== 1) mask + where ones = M.keys $ M.filter (== One) mask maskZeroes :: MaskMap -> Int64 maskZeroes mask = complement $ foldl' setBit zeroBits ones - where ones = M.keys $ M.filter (== 0) mask + where ones = M.keys $ M.filter (== Zero) mask + + +executeInstructions2 instructions = + foldl' executeInstruction2 emtpyMachine instructions + +executeInstruction2 machine (Mask mask) = machine {mMask = mask} +executeInstruction2 machine (Assignment loc value) = machine {mMemory = mem'} + where locs = map encodeMask $ applyAddressMask (mMask machine) $ decodeMask loc + mem = mMemory machine + mem' = foldl' (\m l -> M.insert l value m) mem locs + + +encodeMask :: MaskMap -> Int64 +encodeMask mask = M.foldrWithKey' setBitValue zeroBits mask + where setBitValue _ Zero n = n + setBitValue i One n = setBit n i + +decodeMask :: Int64 -> MaskMap +decodeMask val = M.fromList [ (i, decodeBit $ testBit val i) + | i <- [0..(finiteBitSize val)] + ] + where decodeBit True = One + decodeBit False = Zero + +applyAddressMask :: MaskMap -> MaskMap -> [MaskMap] +applyAddressMask mask address = M.foldrWithKey' applyBit [address] mask +applyBit :: Int -> MaskValue -> [MaskMap] -> [MaskMap] +applyBit _ Zero ms = ms +applyBit k One ms = [ M.insert k One m | m <- ms ] +applyBit k Wild ms = [ M.insert k b m | m <- ms, b <- [Zero, One] ] -- Parse the input file @@ -77,11 +108,13 @@ maskP = maskify <$> ("mask = " *> (many (digit <|> letter))) assignmentP = Assignment <$> ("mem[" *> decimal) <* "] = " <*> decimal maskify :: String -> Instruction -maskify chars = Mask (M.fromList locNums) - where locChars = zip [0..] $ reverse chars - locDigits = filter (isDigit . snd) locChars - locNums = map (\(i, n) -> (i, read @Int [n])) locDigits +maskify chars = Mask (M.fromList locValues) + where mValues = map readMaskChar chars + locValues = zip [0..] $ reverse mValues +readMaskChar '0' = Zero +readMaskChar '1' = One +readMaskChar 'X' = Wild -- successfulParse :: Text -> (Integer, [Maybe Integer]) successfulParse input = diff --git a/data/advent14b.txt b/data/advent14b.txt new file mode 100644 index 0000000..b4b4e06 --- /dev/null +++ b/data/advent14b.txt @@ -0,0 +1,4 @@ +mask = 000000000000000000000000000000X1001X +mem[42] = 100 +mask = 00000000000000000000000000000000X0XX +mem[26] = 1 \ No newline at end of file diff --git a/problems/day14.html b/problems/day14.html new file mode 100644 index 0000000..d08a702 --- /dev/null +++ b/problems/day14.html @@ -0,0 +1,192 @@ + + + + +Day 14 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 28*

      /*2020*/

+ + + +
+ +

--- Day 14: Docking Data ---

As your ferry approaches the sea port, the captain asks for your help again. The computer system that runs this port isn't compatible with the docking program on the ferry, so the docking parameters aren't being correctly initialized in the docking program's memory.

+

After a brief inspection, you discover that the sea port's computer system uses a strange bitmask system in its initialization program. Although you don't have the correct decoder chip handy, you can emulate it in software!

+

The initialization program (your puzzle input) can either update the bitmask or write a value to memory. Values and memory addresses are both 36-bit unsigned integers. For example, ignoring bitmasks for a moment, a line like mem[8] = 11 would write the value 11 to memory address 8.

+

The bitmask is always given as a string of 36 bits, written with the most significant bit (representing 2^35) on the left and the least significant bit (2^0, that is, the 1s bit) on the right. The current bitmask is applied to values immediately before they are written to memory: a 0 or 1 overwrites the corresponding bit in the value, while an X leaves the bit in the value unchanged.

+

For example, consider the following program:

+
mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
+mem[8] = 11
+mem[7] = 101
+mem[8] = 0
+
+

This program starts by specifying a bitmask (mask = ....). The mask it specifies will overwrite two bits in every written value: the 2s bit is overwritten with 0, and the 64s bit is overwritten with 1.

+

The program then attempts to write the value 11 to memory address 8. By expanding everything out to individual bits, the mask is applied as follows:

+
value:  000000000000000000000000000000001011  (decimal 11)
+mask:   XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
+result: 000000000000000000000000000001001001  (decimal 73)
+
+

So, because of the mask, the value 73 is written to memory address 8 instead. Then, the program tries to write 101 to address 7:

+
value:  000000000000000000000000000001100101  (decimal 101)
+mask:   XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
+result: 000000000000000000000000000001100101  (decimal 101)
+
+

This time, the mask has no effect, as the bits it overwrote were already the values the mask tried to set. Finally, the program tries to write 0 to address 8:

+
value:  000000000000000000000000000000000000  (decimal 0)
+mask:   XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
+result: 000000000000000000000000000001000000  (decimal 64)
+
+

64 is written to address 8 instead, overwriting the value that was there previously.

+

To initialize your ferry's docking program, you need the sum of all values left in memory after the initialization program completes. (The entire 36-bit address space begins initialized to the value 0 at every address.) In the above example, only two values in memory are not zero - 101 (at address 7) and 64 (at address 8) - producing a sum of 165.

+

Execute the initialization program. What is the sum of all values left in memory after it completes? (Do not truncate the sum to 36 bits.)

+
+

Your puzzle answer was 15403588588538.

--- Part Two ---

For some reason, the sea port's computer system still can't communicate with your ferry's docking program. It must be using version 2 of the decoder chip!

+

A version 2 decoder chip doesn't modify the values being written at all. Instead, it acts as a memory address decoder. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination memory address in the following way:

+
    +
  • If the bitmask bit is 0, the corresponding memory address bit is unchanged.
  • +
  • If the bitmask bit is 1, the corresponding memory address bit is overwritten with 1.
  • +
  • If the bitmask bit is X, the corresponding memory address bit is floating.
  • +
+

A floating bit is not connected to anything and instead fluctuates unpredictably. In practice, this means the floating bits will take on all possible values, potentially causing many memory addresses to be written all at once!

+

For example, consider the following program:

+
mask = 000000000000000000000000000000X1001X
+mem[42] = 100
+mask = 00000000000000000000000000000000X0XX
+mem[26] = 1
+
+

When this program goes to write to memory address 42, it first applies the bitmask:

+
address: 000000000000000000000000000000101010  (decimal 42)
+mask:    000000000000000000000000000000X1001X
+result:  000000000000000000000000000000X1101X
+
+

After applying the mask, four bits are overwritten, three of which are different, and two of which are floating. Floating bits take on every possible combination of values; with two floating bits, four actual memory addresses are written:

+
000000000000000000000000000000011010  (decimal 26)
+000000000000000000000000000000011011  (decimal 27)
+000000000000000000000000000000111010  (decimal 58)
+000000000000000000000000000000111011  (decimal 59)
+
+

Next, the program is about to write to memory address 26 with a different bitmask:

+
address: 000000000000000000000000000000011010  (decimal 26)
+mask:    00000000000000000000000000000000X0XX
+result:  00000000000000000000000000000001X0XX
+
+

This results in an address with three floating bits, causing writes to eight memory addresses:

+
000000000000000000000000000000010000  (decimal 16)
+000000000000000000000000000000010001  (decimal 17)
+000000000000000000000000000000010010  (decimal 18)
+000000000000000000000000000000010011  (decimal 19)
+000000000000000000000000000000011000  (decimal 24)
+000000000000000000000000000000011001  (decimal 25)
+000000000000000000000000000000011010  (decimal 26)
+000000000000000000000000000000011011  (decimal 27)
+
+

The entire 36-bit address space still begins initialized to the value 0 at every address, and you still need the sum of all values left in memory at the end of the program. In this example, the sum is 208.

+

Execute the initialization program using an emulator for a version 2 decoder chip. What is the sum of all values left in memory after it completes?

+
+

Your puzzle answer was 3260587250457.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1