Day 8: Handheld Halting¶
Classes:
|
Represent an operation corresponding to an instruction. |
|
Represent an instruction of the boot code. |
Functions:
|
Parse a |
|
Parse the boot code given as |
|
Execute the boot code given as |
- class Operation(value)[source]¶
Represent an operation corresponding to an instruction.
Attributes:
- NOP = 'nop'¶
- ACC = 'acc'¶
- JMP = 'jmp'¶
- class Instruction(operation: Operation, argument: int)[source]¶
Represent an instruction of the boot code.
Attributes:
the corresponding operation
the argument to the 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 )