-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc201902_intcode.py
56 lines (39 loc) · 1.31 KB
/
aoc201902_intcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""AoC 2, 2019: 1202 Program Alarm."""
# Standard library imports
import pathlib
import sys
# Advent of Code imports
from aoc import intcode
def parse_data(puzzle_input):
"""Parse input."""
return [int(number) for number in puzzle_input.split(",")]
def part1(data):
"""Solve part 1."""
return run_program(data.copy(), 12, 2)
def part2(data, moon_landing=19690720):
"""Solve part 2."""
base = run_program(data.copy(), 0, 0)
dn = run_program(data.copy(), 1, 0) - base
dv = run_program(data.copy(), 0, 1) - base
noun = (moon_landing - base) // dn
verb = (moon_landing - base) % dn // dv
return noun * 100 + verb
def run_program(program, noun, verb):
"""Modify program with noun and verb and run it.
## Example:
>>> run_program([1, 2, 3, 3, 2, 3, 9, 0, 99, 30, 40, 50], 11, 10)
2700
"""
program[1:3] = [noun, verb]
state = intcode.run_program(program)
return state[0]
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))