Day 8: Handheld Halting

Classes:

Operation(value)

Represent an operation corresponding to an instruction.

Instruction(operation, argument)

Represent an instruction of the boot code.

Functions:

parse_line(line)

Parse a line of the boot code into an instruction.

parse(lines)

Parse the boot code given as lines.

execute_instructions(instructions)

Execute the boot code given as instructions.

class Operation(value)[source]

Represent an operation corresponding to an instruction.

Attributes:

NOP

ACC

JMP

NOP = 'nop'
ACC = 'acc'
JMP = 'jmp'
class Instruction(operation: Operation, argument: int)[source]

Represent an instruction of the boot code.

Attributes:

operation

the corresponding operation

argument

the argument to the operation

operation: Operation

the corresponding operation

argument: int

the argument to the operation

parse_line(line: str) Instruction[source]

Parse a line of the boot code into an instruction.

Requires
  • INSTRUCTION_RE.match(line)

parse(lines: Lines) List[Instruction][source]

Parse the boot code given as lines.

Requires
  • all(INSTRUCTION_RE.match(line) for line in lines)

Ensures
  • len(lines) == len(result)

execute_instructions(instructions: List[Instruction]) Optional[int][source]

Execute the boot code given as instructions.

Returns

The value in the accumulator just before an instruction is run for the second time

Requires
  • all(
        0 <= i + instruction.argument < len(instructions)
        for i, instruction in enumerate(instructions)
        if instruction.operation == Operation.JMP
    )