diff --git a/adventofcode/2019/day11/day11.py b/adventofcode/2019/day11/day11.py index c308a3b..cdee7f8 100644 --- a/adventofcode/2019/day11/day11.py +++ b/adventofcode/2019/day11/day11.py @@ -1,9 +1,4 @@ -# TODO replace PYTHONPATH hack with a proper solution, like making intcode an -# installed module https://stackoverflow.com/a/50194143 -import sys -from pathlib import Path -sys.path.append(str(Path(__file__).absolute().parent.parent / "intcode")) -from intcode import interpret_intcode, Interpreter +from adventofcode.intcode import interpret_intcode, Interpreter def paint(program, initial_state): diff --git a/adventofcode/2019/day13/day13.py b/adventofcode/2019/day13/day13.py index 06ce72c..2f9d1d2 100644 --- a/adventofcode/2019/day13/day13.py +++ b/adventofcode/2019/day13/day13.py @@ -8,8 +8,7 @@ from pathlib import Path from collections import defaultdict, Counter from dataclasses import dataclass -sys.path.append(str(Path(__file__).absolute().parent.parent / "intcode")) -from intcode import interpret_intcode, Interpreter +from adventofcode.intcode import interpret_intcode, Interpreter @dataclass class State: diff --git a/adventofcode/2019/day7/day7.py b/adventofcode/2019/day7/day7.py index 583d59d..4e29b8c 100644 --- a/adventofcode/2019/day7/day7.py +++ b/adventofcode/2019/day7/day7.py @@ -1,10 +1,7 @@ import sys from pathlib import Path - -sys.path.append(str(Path(__file__).absolute().parent.parent / "intcode")) - from itertools import cycle, permutations -from intcode import interpret_intcode, Interpreter +from adventofcode.intcode import interpret_intcode, Interpreter def main(inp): mem = list(map(int, inp.readline().rstrip().split(","))) diff --git a/adventofcode/2019/day9/day9.py b/adventofcode/2019/day9/day9.py index 037beb5..8b39d4c 100644 --- a/adventofcode/2019/day9/day9.py +++ b/adventofcode/2019/day9/day9.py @@ -1,11 +1,4 @@ -# TODO replace PYTHONPATH hack with a proper solution, like making intcode an -# installed module https://stackoverflow.com/a/50194143 -import sys -from pathlib import Path - -sys.path.append(str(Path(__file__).absolute().parent.parent / "intcode")) - -from intcode import interpret_intcode +from adventofcode.intcode import interpret_intcode def main(inp): diff --git a/adventofcode/intcode/README.md b/adventofcode/intcode/README.md new file mode 100644 index 0000000..7451e35 --- /dev/null +++ b/adventofcode/intcode/README.md @@ -0,0 +1,53 @@ +# Intcode + +Interpreter for the Intcode programming language used in Advent of Code 2019. + +## Installation + +The package is automatically installed with the adventofcode package: + +```bash +pip install -e . +``` + +## Usage + +```python +from adventofcode.intcode import interpret_intcode, Interpreter + +# Simple execution +result = interpret_intcode([1, 0, 0, 0, 99]) +print(result.memory) # [2, 0, 0, 0, 99] + +# With stdin +result = interpret_intcode([3, 0, 99, 0], stdin=[42]) +print(result.stdout) # [42] + +# For more control, use the Interpreter class +interpreter = Interpreter([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]) +interpreter.stdin = [8] +interpreter.interpret() +print(interpreter.stdout) # [1] +``` + +## API + +### `interpret_intcode(program, stdin=[])` +Execute an Intcode program and return the Interpreter object. + +- `program`: List of integers representing the Intcode program +- `stdin`: List of integers to use as input + +### `class Interpreter` +Intcode interpreter with more control options. + +- `stdin`: List of integers for input +- `stdout`: List of integers for output +- `memory`: The program memory +- `halted`: Boolean indicating if the program has halted +- `interpret(break_on_input=False, break_on_output=True)`: Run the interpreter + +### Enums +- `Operation`: ADDITION, MULTIPLICATION, INPUT, OUTPUT, JMP_IF_TRUE, JMP_IF_FALSE, LESS_THAN, EQUALS, BASE, TERMINATION +- `Mode`: POSITION, IMMEDIATE, RELATIVE + diff --git a/adventofcode/intcode/__init__.py b/adventofcode/intcode/__init__.py new file mode 100644 index 0000000..ec644a1 --- /dev/null +++ b/adventofcode/intcode/__init__.py @@ -0,0 +1 @@ +from .intcode import interpret_intcode, Interpreter, Operation, Mode diff --git a/adventofcode/2019/intcode/benchmark.py b/adventofcode/intcode/benchmark.py similarity index 100% rename from adventofcode/2019/intcode/benchmark.py rename to adventofcode/intcode/benchmark.py diff --git a/adventofcode/2019/intcode/intcode.py b/adventofcode/intcode/intcode.py similarity index 100% rename from adventofcode/2019/intcode/intcode.py rename to adventofcode/intcode/intcode.py diff --git a/adventofcode/2019/intcode/tests.py b/adventofcode/intcode/tests.py similarity index 100% rename from adventofcode/2019/intcode/tests.py rename to adventofcode/intcode/tests.py