Safe Haskell | None |
---|
Day08
Description
General - A(nother) stack machine problem. Execute instructions until you have reached the end of the program.
Part 1 - The end of the program is reached, when I am hitting a program counter the second time. Return the accumulator.
Part 2 - Need to find a set of instructions that will reach then end of the program (the instruction after the last one). First I am building all possible (better/fixed) instruction sets. Then I run them. They will either terminate with LOOP(-detected) or NORMAL (and also return the accumulator). I will then find the first NORMAL termination and return the accumulator of that termination.
Synopsis
- type Program = (Int, Int)
- type Stack = [Int]
- data Code
- data Instruction = Instruction Code Int
- data Exit
- makeCode :: String -> Code
- makeArgument :: String -> Int
- input :: String -> [Instruction]
- executeInstruction :: Instruction -> Program -> Program
- runProgram :: [Instruction] -> Program -> Stack -> (Int, Exit)
- fixInstruction :: [Instruction] -> [Instruction]
- buildInstructions :: [Instruction] -> [[Instruction]]
- part1 :: [Instruction] -> Int
- part2 :: [Instruction] -> Int
Documentation
All of the op codes.
data Instruction #
The instruction.
Constructors
Instruction Code Int |
Instances
Eq Instruction # | |
Defined in Day08 | |
Show Instruction # | |
Defined in Day08 Methods showsPrec :: Int -> Instruction -> ShowS show :: Instruction -> String showList :: [Instruction] -> ShowS |
The possible exits.
makeArgument :: String -> Int #
Make an argument.
input :: String -> [Instruction] #
Read the input file.
executeInstruction :: Instruction -> Program -> Program #
Execute an instruction and returns the next program (counter/accumulator).
runProgram :: [Instruction] -> Program -> Stack -> (Int, Exit) #
Execute all instructions (recursively) until a LOOP is detected or the program terminates normally (hits the instruction after the last one). Also returns the value of the accumulator at the time/point of termination.
fixInstruction :: [Instruction] -> [Instruction] #
Flipswap the first instruction (if it is a NOPJMP).
buildInstructions :: [Instruction] -> [[Instruction]] #
Build (fixed) instruction lists (from a given instruction list).
part1 :: [Instruction] -> Int #
Solve part1.
part2 :: [Instruction] -> Int #
Solve part2.