Created
January 19, 2016 05:31
-
-
Save briansunter/832f175e20dab6223001 to your computer and use it in GitHub Desktop.
Advent of code Day 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Circuit = Signal Word16 | |
| Wire Label | |
| AndGate Circuit Circuit | |
| OrGate Circuit Circuit | |
| LShiftGate Int Circuit | |
| RShiftGate Int Circuit | |
| NotGate Circuit deriving (Eq, Show, Ord) | |
newtype Label = Label String deriving (Show, Ord, Eq) | |
type CircuitBox = M.Map Label Circuit | |
type Connection = (Label,Circuit) | |
runCircuit :: CircuitBox -> Circuit -> Memo (CircuitBox, Circuit) Word16 Word16 | |
runCircuit b (Signal a) = return a | |
runCircuit b (Wire a) = for2 memo runCircuit b $ (M.!) b a | |
runCircuit b (AndGate r l) = (.&.) <$> for2 memo runCircuit b r <*> for2 memo runCircuit b l | |
runCircuit b (OrGate r l) = (.|.) <$> for2 memo runCircuit b r <*> for2 memo runCircuit b l | |
runCircuit b (LShiftGate i a) = flip shiftL i <$> for2 memo runCircuit b a | |
runCircuit b (RShiftGate i a) = flip shiftR i <$> runCircuit b a | |
runCircuit b (NotGate a) = complement <$> for2 memo runCircuit b a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment