Commit 76f46123 authored by Neil Smith's avatar Neil Smith
Browse files

Day 14 part 2

parent 0438ea03
......@@ -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 =
mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1
\ No newline at end of file
<!DOCTYPE html>
<html lang="en-us">
<meta charset="utf-8"/>
<title>Day 14 - Advent of Code 2020</title>
<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
<link href='//,latin-ext' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="/static/style.css?25"/>
<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
<link rel="shortcut icon" href="/favicon.png"/>
Oh, hello! Funny seeing you here.
I appreciate your enthusiasm, but you aren't going to find much down here.
There certainly aren't clues to any of the puzzles. The best surprises don't
even appear in the source until you unlock them for real.
Please be careful with automated requests; I'm not a massive company, and I can
only take so much traffic. Please be considerate so that everyone gets to play.
If you're curious about how Advent of Code works, it's running on some custom
Perl code. Other than a few integrations (auth, analytics, social media), I
built the whole thing myself, including the design, animations, prose, and all
of the puzzles.
The puzzles are most of the work; preparing a new calendar and a new set of
puzzles each year takes all of my free time for 4-5 months. A lot of effort
went into building this thing - I hope you're enjoying playing it as much as I
enjoyed making it for you!
If you'd like to hang out, I'm @ericwastl on Twitter.
- Eric Wastl
<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2020/about">[About]</a></li><li><a href="/2020/events">[Events]</a></li><li><a href="" target="_blank">[Shop]</a></li><li><a href="/2020/settings">[Settings]</a></li><li><a href="/2020/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">28*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">/*</span><a href="/2020">2020</a><span class="title-event-wrap">*/</span></h1><nav><ul><li><a href="/2020">[Calendar]</a></li><li><a href="/2020/support">[AoC++]</a></li><li><a href="/2020/sponsors">[Sponsors]</a></li><li><a href="/2020/leaderboard">[Leaderboard]</a></li><li><a href="/2020/stats">[Stats]</a></li></ul></nav></div></header>
<div id="sidebar">
<div id="sponsor"><div class="quiet">Our <a href="/2020/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">GitHub</a> - We&apos;re hiring engineers to make GitHub fast. Interested? Email with details of exceptional performance work you&apos;ve done in the past.</div></div>
<script>window.addEventListener('click', function(e,s,r){if('CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(;s.addRange(r);}});</script>
<article class="day-desc"><h2>--- Day 14: Docking Data ---</h2><p>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.</p>
<p>After a brief inspection, you discover that the sea port's computer system uses a strange <a href="" target="_blank">bitmask</a> system in its initialization program. Although you don't have the correct decoder chip handy, you can emulate it in software!</p>
<p>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 <code>mem[8] = 11</code> would write the value <code>11</code> to memory address <code>8</code>.</p>
<p>The bitmask is always given as a string of 36 bits, written with the most significant bit (representing <code>2^35</code>) on the left and the least significant bit (<code>2^0</code>, that is, the <code>1</code>s bit) on the right. The current bitmask is applied to values immediately before they are written to memory: a <code>0</code> or <code>1</code> overwrites the corresponding bit in the value, while an <code>X</code> leaves the bit in the value unchanged.</p>
<p>For example, consider the following program:</p>
mem[8] = 11
mem[7] = 101
mem[8] = 0
<p>This program starts by specifying a bitmask (<code>mask = ....</code>). The mask it specifies will overwrite two bits in every written value: the <code>2</code>s bit is overwritten with <code>0</code>, and the <code>64</code>s bit is overwritten with <code>1</code>.</p>
<p>The program then attempts to write the value <code>11</code> to memory address <code>8</code>. By expanding everything out to individual bits, the mask is applied as follows:</p>
<pre><code>value: 000000000000000000000000000000001011 (decimal 11)
result: 00000000000000000000000000000<em>1</em>0010<em>0</em>1 (decimal 73)
<p>So, because of the mask, the value <code>73</code> is written to memory address <code>8</code> instead. Then, the program tries to write <code>101</code> to address <code>7</code>:</p>
<pre><code>value: 000000000000000000000000000001100101 (decimal 101)
result: 00000000000000000000000000000<em>1</em>1001<em>0</em>1 (decimal 101)
<p>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 <code>0</code> to address <code>8</code>:</p>
<pre><code>value: 000000000000000000000000000000000000 (decimal 0)
result: 00000000000000000000000000000<em>1</em>0000<em>0</em>0 (decimal 64)
<p><code>64</code> is written to address <code>8</code> instead, overwriting the value that was there previously.</p>
<p>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 <code>0</code> at every address.) In the above example, only two values in memory are not zero - <code>101</code> (at address <code>7</code>) and <code>64</code> (at address <code>8</code>) - producing a sum of <em><code>165</code></em>.</p>
<p>Execute the initialization program. <em>What is the sum of all values left in memory after it completes?</em> (Do not truncate the sum to 36 bits.)</p>
<p>Your puzzle answer was <code>15403588588538</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>For some reason, the sea port's computer system still can't communicate with your ferry's docking program. It must be using <em>version 2</em> of the decoder chip!</p>
<p>A version 2 decoder chip doesn't modify the values being written at all. Instead, it acts as a <a href="" target="_blank">memory address decoder</a>. Immediately before a value is written to memory, each bit in the bitmask modifies the corresponding bit of the destination <em>memory address</em> in the following way:</p>
<li>If the bitmask bit is <code>0</code>, the corresponding memory address bit is <em>unchanged</em>.</li>
<li>If the bitmask bit is <code>1</code>, the corresponding memory address bit is <em>overwritten with <code>1</code></em>.</li>
<li>If the bitmask bit is <code>X</code>, the corresponding memory address bit is <span title="Technically, since you're on a boat, they're all floating."><em>floating</em></span>.</li>
<p>A <em>floating</em> bit is not connected to anything and instead fluctuates unpredictably. In practice, this means the floating bits will take on <em>all possible values</em>, potentially causing many memory addresses to be written all at once!</p>
<p>For example, consider the following program:</p>
<pre><code>mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1
<p>When this program goes to write to memory address <code>42</code>, it first applies the bitmask:</p>
<pre><code>address: 000000000000000000000000000000101010 (decimal 42)
mask: 000000000000000000000000000000X1001X
result: 000000000000000000000000000000<em>X1</em>10<em>1X</em>
<p>After applying the mask, four bits are overwritten, three of which are different, and two of which are <em>floating</em>. Floating bits take on every possible combination of values; with two floating bits, four actual memory addresses are written:</p>
<pre><code>000000000000000000000000000000<em>0</em>1101<em>0</em> (decimal 26)
000000000000000000000000000000<em>0</em>1101<em>1</em> (decimal 27)
000000000000000000000000000000<em>1</em>1101<em>0</em> (decimal 58)
000000000000000000000000000000<em>1</em>1101<em>1</em> (decimal 59)
<p>Next, the program is about to write to memory address <code>26</code> with a different bitmask:</p>
<pre><code>address: 000000000000000000000000000000011010 (decimal 26)
mask: 00000000000000000000000000000000X0XX
result: 00000000000000000000000000000001<em>X</em>0<em>XX</em>
<p>This results in an address with three floating bits, causing writes to <em>eight</em> memory addresses:</p>
<pre><code>00000000000000000000000000000001<em>0</em>0<em>00</em> (decimal 16)
00000000000000000000000000000001<em>0</em>0<em>01</em> (decimal 17)
00000000000000000000000000000001<em>0</em>0<em>10</em> (decimal 18)
00000000000000000000000000000001<em>0</em>0<em>11</em> (decimal 19)
00000000000000000000000000000001<em>1</em>0<em>00</em> (decimal 24)
00000000000000000000000000000001<em>1</em>0<em>01</em> (decimal 25)
00000000000000000000000000000001<em>1</em>0<em>10</em> (decimal 26)
00000000000000000000000000000001<em>1</em>0<em>11</em> (decimal 27)
<p>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 <em><code>208</code></em>.</p>
<p>Execute the initialization program using an emulator for a version 2 decoder chip. <em>What is the sum of all values left in memory after it completes?</em></p>
<p>Your puzzle answer was <code>3260587250457</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
<p>At this point, you should <a href="/2020">return to your Advent calendar</a> and try another puzzle.</p>
<p>If you still want to see it, you can <a href="14/input" target="_blank">get your puzzle input</a>.</p>
<p>You can also <span class="share">[Share<span class="share-content">on
<a href=";url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F14&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
<a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=I%27ve+completed+%22Docking+Data%22+%2D+Day+14+%2D+Advent+of+Code+2020+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F14'}else{return false;}" target="_blank">Mastodon</a
></span>]</span> this puzzle.</p>
<!-- ga -->
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
ga('create', 'UA-69522494-1', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
<!-- /ga -->
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment