def calc(log, values, first_input): from program import Program ticker = [int(x) for x in values[0].split(",")] prog = Program(ticker, log) prog.add_to_input(first_input) prog.tick_till_end() return ",".join([str(x) for x in list(prog.output)[::-1]])
def calc(log, values, mode, animate=False): from program import Program from grid import Grid from collections import deque ticker = [int(x) for x in values[0].split(",")] program = Program(ticker, log) x, y = 0, 0 changed = 0 dirs = deque([(0, -1), (1, 0), (0, 1), (-1, 0)]) grid = Grid() if mode == 2: grid.set(1, 0, 0) while True: if len(program.output) > 0: paint, change_dir = program.get_output(2) dirs.rotate(-1 if change_dir == 1 else 1) if not grid.value_isset(x, y): changed += 1 if animate: old = grid.get(x, y) grid.set("Star", x, y) grid.save_frame() grid.set(old, x, y) grid.set(paint, x, y) x += dirs[0][0] y += dirs[0][1] program.add_to_input(grid.get(x, y)) program.tick_till_end() if not program.flag_running: break if mode == 2: log("") grid.show_grid(log) grid.decode_grid(log) log("") if animate: Grid.clear_frames() grid.draw_frames(repeat_final=40) return changed
def calc(log, values, feedback, debug=False): from program import Program import itertools ticker = [int(x) for x in values[0].split(",")] max_output = 0 best_perm = None best_frames = None for cur in itertools.permutations( [5, 6, 7, 8, 9] if feedback else [0, 1, 2, 3, 4]): progs = [] first = True for x in cur: prog = Program(ticker, log) if first: first = False if debug: prog.save_frames() prog.add_to_input(x) if len(progs) > 0: prog.hook_up_output(progs[-1]) progs.append(prog) progs[0].hook_up_output(progs[-1]) progs[0].add_to_input(0) while progs[4].flag_running: for prog in progs: prog.tick_till_end() if progs[4].last_output > max_output: max_output = progs[4].last_output best_perm = cur best_frames = progs[0].frames if debug: log(best_perm) states = {} for cur in best_frames: if cur[0] not in {"input", "output"}: for key in cur[1]: if key not in states: states[key] = set() for cur in best_frames: if cur[0] not in {"input", "output"}: for key in states: states[key].add(cur[1].get(key, None)) changes = [] for cur in sorted(states): if len(states[cur]) > 1: changes.append(cur) class SaveLine: def __init__(self): self.last = "" def show(self, value): self.last = value s = SaveLine() for cur in best_frames: if cur[0] not in {"input", "output"}: row = [] for x in changes: row.append("%d:%d" % (x, cur[1].get(x, None))) Program.debug_line(s, ticker, cur[0]) print("%-75s -- %s" % (s.last, ", ".join(row))) else: if cur[0] == "input": print("%sProvided input of %d" % (" " * 31, cur[1])) else: print("%sGot output of %d" % (" " * 31, cur[1])) return max_output
def calc(log, values, play_game, animate=False): from program import Program from grid import Grid ticker = [int(x) for x in values[0].split(",")] program = Program(ticker, log) if play_game: program.ticker[0] = 2 score = 0 grid = Grid() if not play_game: program.tick_till_end() while len(program.output) >= 3: x, y, tile = program.get_output(3) if x != -1: grid.set(tile, x, y) elif not animate: while program.flag_running: program.tick_till_end() while len(program.output) >= 3: x, y, tile = program.get_output(3) if tile == 4: ball = (x, y) if tile == 3: paddle = (x, y) if x == -1: score = tile if program.flag_input_dry: if ball[0] < paddle[0]: program.add_to_input(-1) elif ball[0] > paddle[0]: program.add_to_input(1) else: program.add_to_input(0) else: ball = (0, 0) paddle = (0, 0) while program.flag_running: copy = program.make_copy() ball_trail = [] while copy.flag_running: copy.tick() if copy.flag_input_dry: copy.add_to_input(0) ball_trail.append(ball) if paddle is not None and len(ball_trail) > 2: if ball_trail[-1][1] == paddle[1] - 1 and ball_trail[ -2][1] == paddle[1] - 2: break elif len(copy.output) == 3: x, y, tile = copy.get_output(3) if tile == 3: paddle = (x, y) elif tile == 4: ball = (x, y) paddle_move = None steps = 0 while program.flag_running: program.tick() while len(program.output) >= 3: x, y, tile = program.get_output(3) if tile == 4: ball = (x, y) if tile == 3: paddle = (x, y) if x == -1: score = tile else: grid.set(tile, x, y) if not program.flag_running: break if steps == len(ball_trail): break if program.flag_input_dry: grid.save_frame(extra_text=["SCORE: | %07d" % (score, )]) steps += 1 if paddle_move is None: paddle_move = paddle[0] - ball_trail[-1][0] if steps > 0 and copy.flag_running and paddle_move != 0: if paddle_move > 0: program.add_to_input(-1) paddle_move -= 1 elif paddle_move < 0: program.add_to_input(1) paddle_move += 1 else: program.add_to_input(0) if animate: grid.save_frame(extra_text=[ "SCORE: | %07d | HIGH SCORE !!" % (score, ), "GAME OVER" ]) Grid.clear_frames() grid.draw_frames(color_map={ 0: (0, 0, 0), 1: (255, 255, 255), 2: (128, 128, 128), 3: (255, 64, 64), 4: (128, 128, 255), }, repeat_final=30 * 5) Grid.make_animation(file_format="mp4", output_name="animation_%02d" % (get_desc()[0], )) if play_game: return score count = 0 for cur in grid.grid: value = grid.grid[cur] if value in {2}: count += 1 return count
def getxy(x, y): prog = Program(ticker, log) prog.add_to_input(x) prog.add_to_input(y) prog.tick_till_end() return prog.get_output()