From c94e9a48c6130661b2891ca56381d11bab681afd Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 24 Dec 2018 09:30:28 +0000 Subject: [PATCH] Day 19 at last --- data/advent19-small.txt | 8 ++ data/advent19.txt | 37 ++++++++ problems/day19.html | 155 ++++++++++++++++++++++++++++++ src/advent19/advent19.hs | 197 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 397 insertions(+) create mode 100644 data/advent19-small.txt create mode 100644 data/advent19.txt create mode 100644 problems/day19.html create mode 100644 src/advent19/advent19.hs diff --git a/data/advent19-small.txt b/data/advent19-small.txt new file mode 100644 index 0000000..d7d2a21 --- /dev/null +++ b/data/advent19-small.txt @@ -0,0 +1,8 @@ +#ip 0 +seti 5 0 1 +seti 6 0 2 +addi 0 1 0 +addr 1 2 3 +setr 1 0 0 +seti 8 0 4 +seti 9 0 5 diff --git a/data/advent19.txt b/data/advent19.txt new file mode 100644 index 0000000..3ff582b --- /dev/null +++ b/data/advent19.txt @@ -0,0 +1,37 @@ +#ip 5 +addi 5 16 5 +seti 1 8 3 +seti 1 1 1 +mulr 3 1 4 +eqrr 4 2 4 +addr 4 5 5 +addi 5 1 5 +addr 3 0 0 +addi 1 1 1 +gtrr 1 2 4 +addr 5 4 5 +seti 2 7 5 +addi 3 1 3 +gtrr 3 2 4 +addr 4 5 5 +seti 1 5 5 +mulr 5 5 5 +addi 2 2 2 +mulr 2 2 2 +mulr 5 2 2 +muli 2 11 2 +addi 4 8 4 +mulr 4 5 4 +addi 4 20 4 +addr 2 4 2 +addr 5 0 5 +seti 0 4 5 +setr 5 8 4 +mulr 4 5 4 +addr 5 4 4 +mulr 5 4 4 +muli 4 14 4 +mulr 4 5 4 +addr 2 4 2 +seti 0 7 0 +seti 0 9 5 diff --git a/problems/day19.html b/problems/day19.html new file mode 100644 index 0000000..7be7262 --- /dev/null +++ b/problems/day19.html @@ -0,0 +1,155 @@ + + + + +Day 19 - Advent of Code 2018 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 38*

          2018

+ + + +
+

--- Day 19: Go With The Flow ---

With the Elves well on their way constructing the North Pole base, you turn your attention back to understanding the inner workings of programming the device.

+

You can't help but notice that the device's opcodes don't contain any flow control like jump instructions. The device's manual goes on to explain:

+

"In programs where flow control is required, the instruction pointer can be bound to a register so that it can be manipulated directly. This way, setr/seti can function as absolute jumps, addr/addi can function as relative jumps, and other opcodes can cause truly fascinating effects."

+

This mechanism is achieved through a declaration like #ip 1, which would modify register 1 so that accesses to it let the program indirectly access the instruction pointer itself. To compensate for this kind of binding, there are now six registers (numbered 0 through 5); the five not bound to the instruction pointer behave as normal. Otherwise, the same rules apply as the last time you worked with this device.

+

When the instruction pointer is bound to a register, its value is written to that register just before each instruction is executed, and the value of that register is written back to the instruction pointer immediately after each instruction finishes execution. Afterward, move to the next instruction by adding one to the instruction pointer, even if the value in the instruction pointer was just updated by an instruction. (Because of this, instructions must effectively set the instruction pointer to the instruction before the one they want executed next.)

+

The instruction pointer is 0 during the first instruction, 1 during the second, and so on. If the instruction pointer ever causes the device to attempt to load an instruction outside the instructions defined in the program, the program instead immediately halts. The instruction pointer starts at 0.

+

It turns out that this new information is already proving useful: the CPU in the device is not very powerful, and a background process is occupying most of its time. You dump the background process' declarations and instructions to a file (your puzzle input), making sure to use the names of the opcodes rather than the numbers.

+

For example, suppose you have the following program:

+
#ip 0
+seti 5 0 1
+seti 6 0 2
+addi 0 1 0
+addr 1 2 3
+setr 1 0 0
+seti 8 0 4
+seti 9 0 5
+
+

When executed, the following instructions are executed. Each line contains the value of the instruction pointer at the time the instruction started, the values of the six registers before executing the instructions (in square brackets), the instruction itself, and the values of the six registers after executing the instruction (also in square brackets).

+
ip=0 [0, 0, 0, 0, 0, 0] seti 5 0 1 [0, 5, 0, 0, 0, 0]
+ip=1 [1, 5, 0, 0, 0, 0] seti 6 0 2 [1, 5, 6, 0, 0, 0]
+ip=2 [2, 5, 6, 0, 0, 0] addi 0 1 0 [3, 5, 6, 0, 0, 0]
+ip=4 [4, 5, 6, 0, 0, 0] setr 1 0 0 [5, 5, 6, 0, 0, 0]
+ip=6 [6, 5, 6, 0, 0, 0] seti 9 0 5 [6, 5, 6, 0, 0, 9]
+
+

In detail, when running this program, the following events occur:

+
    +
  • The first line (#ip 0) indicates that the instruction pointer should be bound to register 0 in this program. This is not an instruction, and so the value of the instruction pointer does not change during the processing of this line.
  • +
  • The instruction pointer contains 0, and so the first instruction is executed (seti 5 0 1). It updates register 0 to the current instruction pointer value (0), sets register 1 to 5, sets the instruction pointer to the value of register 0 (which has no effect, as the instruction did not modify register 0), and then adds one to the instruction pointer.
  • +
  • The instruction pointer contains 1, and so the second instruction, seti 6 0 2, is executed. This is very similar to the instruction before it: 6 is stored in register 2, and the instruction pointer is left with the value 2.
  • +
  • The instruction pointer is 2, which points at the instruction addi 0 1 0. This is like a relative jump: the value of the instruction pointer, 2, is loaded into register 0. Then, addi finds the result of adding the value in register 0 and the value 1, storing the result, 3, back in register 0. Register 0 is then copied back to the instruction pointer, which will cause it to end up 1 larger than it would have otherwise and skip the next instruction (addr 1 2 3) entirely. Finally, 1 is added to the instruction pointer.
  • +
  • The instruction pointer is 4, so the instruction setr 1 0 0 is run. This is like an absolute jump: it copies the value contained in register 1, 5, into register 0, which causes it to end up in the instruction pointer. The instruction pointer is then incremented, leaving it at 6.
  • +
  • The instruction pointer is 6, so the instruction seti 9 0 5 stores 9 into register 5. The instruction pointer is incremented, causing it to point outside the program, and so the program ends.
  • +
+

What value is left in register 0 when the background process halts?

+
+

Your puzzle answer was 2640.

--- Part Two ---

A new background process immediately spins up in its place. It appears identical, but on closer inspection, you notice that this time, register 0 started with the value 1.

+

What value is left in register 0 when this new background process halts?

+
+

Your puzzle answer was 27024480.

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 diff --git a/src/advent19/advent19.hs b/src/advent19/advent19.hs new file mode 100644 index 0000000..2ffc3d7 --- /dev/null +++ b/src/advent19/advent19.hs @@ -0,0 +1,197 @@ +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} + + +import Debug.Trace + +-- import Prelude hiding ((++)) +import Data.Text (Text) +import qualified Data.Text as T +import qualified Data.Text.IO as TIO + +import Data.Void (Void) +import Text.Megaparsec hiding (State) +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import qualified Data.Map.Strict as M +import Data.Map.Strict ((!)) +import Data.Bits ((.&.), (.|.)) + +import Control.Monad (when) +import Control.Monad.State.Lazy +import Control.Monad.Reader +import Control.Monad.Writer + +type Memory = M.Map Integer Integer + +data Location = Literal Integer | Register Integer deriving (Show, Eq) +data Instruction = + Addr Integer Integer Integer + | Addi Integer Integer Integer + | Mulr Integer Integer Integer + | Muli Integer Integer Integer + | Banr Integer Integer Integer + | Bani Integer Integer Integer + | Borr Integer Integer Integer + | Bori Integer Integer Integer + | Setr Integer Integer Integer + | Seti Integer Integer Integer + | Gtir Integer Integer Integer + | Gtri Integer Integer Integer + | Gtrr Integer Integer Integer + | Eqir Integer Integer Integer + | Eqri Integer Integer Integer + | Eqrr Integer Integer Integer + deriving (Eq, Show, Ord) + + +data Machine = Machine { _registers :: M.Map Integer Integer + , _pc :: Int + -- , _pcReg :: Integer + } + deriving (Show, Eq) + +type ProgrammedMachine = WriterT [Integer] (ReaderT (Integer, [Instruction]) (State Machine)) () + +emptyMachine = Machine {_registers = M.fromList (zip [0..5] (repeat 0)), + _pc = 0} + +main :: IO () +main = do + text <- TIO.readFile "data/advent19.txt" + let (ip, instrs) = successfulParse text + print (ip, instrs) + -- print $ part1 ip instrs + print $ sum [i | i <- [1..1032], 1032 `mod` i == 0] + -- print $ part2 ip instrs + print $ sum [i | i <- [1..10551432], 10551432 `mod` i == 0] + +part1 ip instructions = + runState ( + runReaderT ( + runWriterT executeInstructions + ) + (ip, instructions) + ) + emptyMachine + +part2 ip instructions = + runState ( + runReaderT ( + runWriterT executeInstructions + ) + (ip, instructions) + ) + m2 + where emptyRegisters = _registers emptyMachine + m2 = emptyMachine {_registers = M.insert 0 1 emptyRegisters} + +executeInstructions = + do (_, instrs) <- ask + m <- get + when (_pc m >= 0 && _pc m < length instrs) + $ + do executeInstruction + executeInstructions + +executeInstruction :: ProgrammedMachine +executeInstruction = + do (pcIs, instrs) <- ask + m <- get + let instr = instrs!!(_pc m) + let memory0 = _registers m + let memory1 = M.insert pcIs (fromIntegral (_pc m)) memory0 + let memory2 = if canPeep1 instrs (_pc m) memory1 -- sample1 == sample1Target + then memoryPeep1 memory1 + else perform instr memory1 + -- let memory2 = perform instr memory1 + let pc' = fromIntegral ((memory2!pcIs) + 1) + -- let aaa = trace ("pc: " ++ show (_pc m) ++ " m0: " ++ show memory0 ++ " m1: " ++ show memory1 ++ "m2: " ++ show memory2 ++ "pc': " ++ show pc') $! True + let m' = m {_registers = memory2, _pc = pc'} + put m' + where sample1 pcVal instructions = take (length sample1Target) $ drop pcVal instructions + sample1Target = [ Mulr 3 1 4 + , Eqrr 4 2 4 + , Addr 4 5 5 + , Addi 5 1 5 + , Addr 3 0 0 + , Addi 1 1 1 + , Gtrr 1 2 4 + , Addr 5 4 5 + , Seti 2 7 5 + ] + canPeep1 instructions pcVal mem = False -- ((sample1 pcVal instructions) == sample1Target) && ((mem!4) == 0) + memoryPeep1 mem = M.union (M.fromList [(0, mem!0 + (if (((mem!2) `mod` (mem!3)) == 0) then mem!3 else 0)), (1, mem!2), (4, mem!2)]) mem + -- M.insert 0 (mem!0 + mem!3) $ M.insert 1 (mem!2) $ M.insert 4 (mem!2) mem + + +perform :: Instruction -> Memory -> Memory +-- perform instr memory | ((memory!5 == 7) || ((memory!5 == 3) && (memory!1 == 1))) && (trace ("Perform " ++ show instr ++ " " ++ show memory) False) = undefined +perform instr memory | trace ("Perform " ++ show instr ++ " " ++ show memory) False = undefined +perform (Addr a b c) memory = M.insert c (memory!a + memory!b) memory +perform (Addi a b c) memory = M.insert c (memory!a + b) memory +perform (Mulr a b c) memory = M.insert c (memory!a * memory!b) memory +perform (Muli a b c) memory = M.insert c (memory!a * b) memory +perform (Banr a b c) memory = M.insert c (memory!a .&. memory!b) memory +perform (Bani a b c) memory = M.insert c (memory!a .&. b) memory +perform (Borr a b c) memory = M.insert c (memory!a .|. memory!b) memory +perform (Bori a b c) memory = M.insert c (memory!a .|. b) memory +perform (Setr a b c) memory = M.insert c (memory!a) memory +perform (Seti a b c) memory = M.insert c a memory +perform (Gtir a b c) memory = M.insert c (if a > (memory!b) then 1 else 0) memory +perform (Gtri a b c) memory = M.insert c (if (memory!a) > b then 1 else 0) memory +perform (Gtrr a b c) memory = M.insert c (if (memory!a) > (memory!b) then 1 else 0) memory +perform (Eqir a b c) memory = M.insert c (if a == memory!b then 1 else 0) memory +perform (Eqri a b c) memory = M.insert c (if (memory!a) == b then 1 else 0) memory +perform (Eqrr a b c) memory = M.insert c (if (memory!a) == (memory!b) then 1 else 0) memory + + +-- evaluate :: Machine -> Location -> Integer +-- evaluate _ (Literal i) = i +-- evaluate m (Register r) = M.findWithDefault 0 r (registers m) + + + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty + +lexeme = L.lexeme sc +integer = lexeme L.decimal +symb = L.symbol sc + + +instructionsP = (,) <$> headerP <*> many instructionP +instructionP = choice [ addrP, addiP, mulrP, muliP, banrP, baniP, + borrP, boriP, setrP, setiP, gtirP, gtriP, gtrrP, + eqirP, eqriP, eqrrP ] + +headerP = symb "#ip" *> integer + +addrP = Addr <$> (try (symb "addr") *> integer) <*> integer <*> integer +addiP = Addi <$> (try (symb "addi") *> integer) <*> integer <*> integer +mulrP = Mulr <$> (try (symb "mulr") *> integer) <*> integer <*> integer +muliP = Muli <$> (try (symb "muli") *> integer) <*> integer <*> integer +banrP = Banr <$> (try (symb "banr") *> integer) <*> integer <*> integer +baniP = Bani <$> (try (symb "bani") *> integer) <*> integer <*> integer +borrP = Borr <$> (try (symb "borr") *> integer) <*> integer <*> integer +boriP = Bori <$> (try (symb "bori") *> integer) <*> integer <*> integer +setrP = Setr <$> (try (symb "setr") *> integer) <*> integer <*> integer +setiP = Seti <$> (try (symb "seti") *> integer) <*> integer <*> integer +gtirP = Gtir <$> (try (symb "gtir") *> integer) <*> integer <*> integer +gtriP = Gtri <$> (try (symb "gtri") *> integer) <*> integer <*> integer +gtrrP = Gtrr <$> (try (symb "gtrr") *> integer) <*> integer <*> integer +eqirP = Eqir <$> (try (symb "eqir") *> integer) <*> integer <*> integer +eqriP = Eqri <$> (try (symb "eqri") *> integer) <*> integer <*> integer +eqrrP = Eqrr <$> (try (symb "eqrr") *> integer) <*> integer <*> integer + +successfulParse :: Text -> (Integer, [Instruction]) +successfulParse input = + case parse instructionsP "input" input of + Left _error -> (0, []) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right instructions -> instructions \ No newline at end of file -- 2.34.1