Safe HaskellNone

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

Documentation

type Program = (Int, Int) #

type Stack = [Int] #

data Code #

All of the op codes.

Constructors

NOP 
ACC 
JMP 

Instances

Instances details
Eq Code # 
Instance details

Defined in Day08

Methods

(==) :: Code -> Code -> Bool

(/=) :: Code -> Code -> Bool

Show Code # 
Instance details

Defined in Day08

Methods

showsPrec :: Int -> Code -> ShowS

show :: Code -> String

showList :: [Code] -> ShowS

data Instruction #

The instruction.

Constructors

Instruction Code Int 

Instances

Instances details
Eq Instruction # 
Instance details

Defined in Day08

Methods

(==) :: Instruction -> Instruction -> Bool

(/=) :: Instruction -> Instruction -> Bool

Show Instruction # 
Instance details

Defined in Day08

Methods

showsPrec :: Int -> Instruction -> ShowS

show :: Instruction -> String

showList :: [Instruction] -> ShowS

data Exit #

The possible exits.

Constructors

NORMAL 
LOOP 

Instances

Instances details
Eq Exit # 
Instance details

Defined in Day08

Methods

(==) :: Exit -> Exit -> Bool

(/=) :: Exit -> Exit -> Bool

Show Exit # 
Instance details

Defined in Day08

Methods

showsPrec :: Int -> Exit -> ShowS

show :: Exit -> String

showList :: [Exit] -> ShowS

makeCode :: String -> Code #

Make an ops code.

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.