def solve_puzzle_one(self): line = self.load_only_input_line() processor = IntcodeProcessor(program_str=line) num_blocks = 0 while processor.last_opcode != 99: processor.get_next_output() processor.get_next_output() v = processor.get_next_output() if v == 2: num_blocks += 1 return num_blocks
def solve_puzzle_two(self): def determine_input(): if ball[0] < paddle[0]: return -1 elif ball[0] == paddle[0]: return 0 else: return 1 line = self.load_only_input_line() processor = IntcodeProcessor(program_str=line, input_func=determine_input) processor.program[0] = 2 board = {} ball = (0, 0) paddle = (0, 0) score = 0 while processor.last_opcode != 99: x = processor.get_next_output() y = processor.get_next_output() v = processor.get_next_output() point = x, y if point == (-1, 0): score = v else: board[point] = v if v == 3: paddle = point if v == 4: ball = point return score
class Springdroid(object): instructions = deque() def __init__(self, program_str): self.processor = IntcodeProcessor(program_str=program_str, input_func=self._input_func) def _input_func(self): if len(self.instructions): return self.instructions.popleft() else: return None def add_instruction(self, instruction): for c in instruction: self.instructions.append(ord(c)) self.instructions.append(ord('\n')) def walk(self, debug=False): self.add_instruction('WALK') return self._execute(debug) def run(self, debug=False): self.add_instruction('RUN') return self._execute(debug) def _execute(self, debug): self.processor.reset() output = '' while True: out = self.processor.get_next_output() if self.processor.last_opcode != 99: if out > 127: if debug: print(output) output = out else: output += chr(out) else: return output