def main(): program = Intcode() program.load_from_file('inputs/day_02_input.txt') program.dsky(12, 2) program.execute() print("\nFinal value at position 0: {0}\n".format(program.memory[0])) return program.memory[0]
def main(): program = Intcode() program.load_from_file('inputs/day_05_input.txt') program.input(5) program.execute() diagnostic_code = program.output() print("\nDiagnostic code is: {0}\n".format(diagnostic_code)) return diagnostic_code
def main(): program = Intcode() program.load_from_file('inputs/day_09_input.txt') program.input(2) program.execute() while program.output_buffer: coordinates = program.output() print("\nDistress signal coordinates are: {0}\n".format(coordinates)) return coordinates
def map_scaffolding(): program = Intcode() program.load_from_file('inputs/day_17_input.txt') program.execute() output = program.output_buffer outstr = ''.join(chr(x) for x in output) lines = outstr.split('\n') lines = [line for line in lines if line] return lines
def main(): program = Intcode() program.load_from_file('inputs/day_09_input.txt') program.input(1) program.execute() while program.output_buffer: key_code = program.output() print("\nBOOST key code is: {0}\n".format(key_code)) return key_code
def main(): screen = {} program = Intcode() program.load_from_file('inputs/day_13_input.txt') program.execute() while program.output_buffer: x = program.output() y = program.output() v = program.output() screen[(x, y)] = v n_blocks = sum(x == 2 for x in screen.values()) print("\nThere are a total of {0} blocks\n".format(n_blocks)) return n_blocks
def main(): output = 19690720 program = Intcode() program.load_from_file('inputs/day_02_input.txt') for noun, verb in \ [(noun, verb) for noun in range(0, 100) for verb in range(0, 100)]: program.rewind() program.dsky(noun, verb) program.execute() if program.memory[0] == output: break program_code = 100 * noun + verb print("\nThe program outputting {0} is: {1:04d} " "(noun {2:02d} - verb {3:02d})\n".format(program.memory[0], program_code, noun, verb)) return program_code
def main(display=False): """ We'll only use LEFT rotations, since R = LLL, so we'll recover it later. We'll also work with the map spread out on a single line In this configuration, L-R is -/+ 1 and U-D is -/+ N, (N is a line length) First we trace the scaffold with the strategy "keep going forward until you can only turn, then turn - all until you can only backtrack". Then we try to compress the obtained command sequence, and give the result to the Intcode computer to process. """ # Prepare the map lines = map_scaffolding() N = len(lines[0]) mapstr = ''.join(lines) # Circular list with main directions in CW order (Up Right Down Left) directions = deque([-N, +1, +N, -1]) def turn_left(): directions.rotate(+1) # This "rotates the robot" def front(): return directions[0] # This indicates "the direction the robot faces" def next_tile(position, last_visited): """ Returns true if the tile in front of the robot is a valid scaffold tile We make sure that it's not a recently visited scaffold, so we're not just backtracking, hence the "valid" """ try: new_position = position + front() if mapstr[new_position] == '#' and new_position != last_visited: # Also check that we're not crossing the L or R borders if not (((position % N) == 0 and front() == -1) or ((position % N) == N - 1 and front() == +1)): return True except IndexError: pass return False # PATHFINDING & COMMAND BUILDING # Find the robot robot_pos = re.search(r'[^\.\#]', mapstr).start() last_visited = None robot_dir = {'^': -N, '>': +1, 'v': +N, '<': -1}[mapstr[robot_pos]] # Align reference system, since front() by default is pointing Up while front() != robot_dir: turn_left() # Build command sequence as a list commands = [] while True: rotation_timeout = 0 while not next_tile(robot_pos, last_visited) and rotation_timeout < 4: turn_left() commands.append('L') rotation_timeout = rotation_timeout + 1 if rotation_timeout == 4: break # We're spinning in circles, nowhere left to go... counter = 0 while next_tile(robot_pos, last_visited): last_visited = robot_pos robot_pos = robot_pos + front() counter = counter + 1 commands.append(str(counter)) # Cut away last Ls while spinning in cicles and convert LLL into R while commands[-1] == 'L': commands.pop(-1) replace_sublist(commands, ['L', 'L', 'L'], ['R']) # Compress commands symbols = ['A', 'B', 'C'] sequence, functions = compress(commands, symbols, maxlen=20) # RUNNING THE INTCODE PROGRAM program = Intcode() program.load_from_file('inputs/day_17_input.txt') program.memory[0] = 2 # Manual override video_feed = {True: 'y', False: 'n'}[display] # Build program input program_input = '{0}\n'.format(sequence) for s in symbols: program_input = program_input + '{0}\n'.format(functions[s]) program_input = program_input + '{0}\n'.format(video_feed) for x in program_input: program.input(ord(x)) # Run the program if display: # Live video feed screen_buffer = '' while not program.ishalted: program.step() if program.output_buffer: screen_buffer += chr(program.output()) # TODO: handle values outside of chr range if screen_buffer[-2:] == '\n\n': Display.clear() print(screen_buffer, end='') screen_buffer = '' dust = ord(screen_buffer[-1]) # Reconstruct dust from last output else: # Output only program.execute() dust = program.output_buffer[-1] # Output print("\nFound a solution with sequence: {0}".format(sequence)) for symbol in symbols: print("{0}: {1}".format(symbol, functions[symbol])) print("\nTotal dust collected: {0}\n".format(dust)) return dust