sln_2020_14
Copyright(c) Eric Mertens 2020
LicenseISC
Maintaineremertens@gmail.com
Safe HaskellNone
LanguageHaskell2010

Main

Description

https://adventofcode.com/2020/day/14

>>> :set -XQuasiQuotes
>>> :{
let cmds = [format|- ((mask = M*|mem[%u] = %u)%n)*|]
      "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0Xn
      mem[8] = 11n
      mem[7] = 101n
      mem[8] = 0n"
in run1 [] IntMap.empty cmds
:}
165

>>> :{
let cmds = [format|- ((mask = M*|mem[%u] = %u)%n)*|]
      "mask = 000000000000000000000000000000X1001Xn
      mem[42] = 100n
      mask = 00000000000000000000000000000000X0XXn
      mem[26] = 1n"
in run2 [] IntMap.empty cmds
:}
208

Synopsis

Documentation

type Cmd = Either [M] (Int, Int) Source #

data M Source #

Constructors

M1 
M0 
MX 

Instances

Instances details
Show M Source # 
Instance details

Defined in Main

Methods

showsPrec :: Int -> M -> ShowS #

show :: M -> String #

showList :: [M] -> ShowS #

main :: IO () Source #

>>> :main
17934269678453
3440662844064

run1 Source #

Arguments

:: [M]

initial mask

-> IntMap Int

initial memory

-> [Cmd]

program statements

-> Int 

Simulate the computer using the mask1 rule.

mask1 Source #

Arguments

:: Int

target value

-> Int

bit index of beginning of mask

-> [M] 
-> Int 

Apply a mask where 1 and 0 overwrite bits.

>>> mask1 11 6 [M1,MX,MX,MX,MX,M0,MX]
73
>>> mask1 101 6 [M1,MX,MX,MX,MX,M0,MX]
101
>>> mask1 0 6 [M1,MX,MX,MX,MX,M0,MX]
64

run2 Source #

Arguments

:: [M]

initial mask

-> IntMap Int

initial memory

-> [Cmd]

program statements

-> Int

sum of memory

Simulate the computer using the mask2 rule.

mask2 Source #

Arguments

:: Int

target value

-> Int

bit index of beginning of mask

-> [M] 
-> [Int] 

Apply a mask where I overwrites and X takes both bit values.

>>> mask2 42 5 [MX,M1,M0,M0,M1,MX]
[59,27,58,26]