class Robot: def __init__(self, int_code_program: List[int], hull) -> None: self.hull = hull self.x, self.y = 0, 0 self.direction = 0 self.solver = Solver(int_code_program) self.seen = set() def paint(self) -> None: while not self.solver.is_finished: color = self.solver.solve(self.hull[self.x, self.y]) turn = self.solver.solve() self.hull[self.x, self.y] = color self.seen.add((self.x, self.y)) self.move(-1 if turn == 0 else 1) def move(self, turn: int) -> None: self.direction = (self.direction + turn) % 4 if self.direction == 0: self.y -= 1 elif self.direction == 1: self.x += 1 elif self.direction == 2: self.y += 1 elif self.direction == 3: self.x -= 1
def __init__(self, int_code_program: List[int], hull) -> None: self.dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)] self.hull = hull self.x, self.y = 0, 0 self.direction = 0 self.solver = Solver(int_code_program) self.seen = set()
class Robot: def __init__(self, int_code_program: List[int], hull) -> None: self.dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)] self.hull = hull self.x, self.y = 0, 0 self.direction = 0 self.solver = Solver(int_code_program) self.seen = set() def paint(self) -> None: while not self.solver.is_finished: color = self.solver.solve(self.hull[self.x, self.y]) turn = self.solver.solve() self.hull[self.x, self.y] = color self.seen.add((self.x, self.y)) self.move(-1 if turn == 0 else 1) def move(self, turn: int) -> None: self.direction = (self.direction + turn) % 4 self.x += self.dirs[self.direction][0] self.y += self.dirs[self.direction][1]
from collections import defaultdict from int_code_solver import Solver with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) screen = defaultdict(lambda: "?") solver = Solver(int_code_program) x, y = 0, 0 while output := solver.solve(): if chr(output) == "\n": y += 1 x = 0 else: screen[x, y] = chr(output) x += 1 min_y = min(y for x, y in screen) max_y = max(y for x, y in screen) min_x = min(x for x, y in screen) max_x = max(x for x, y in screen) total_sum = 0 for y in range(min_y, max_y + 1): for x in range(min_x, max_x + 1): if screen[x, y] == "#" \ and x - 1 >= min_x and x + 1 <= max_x \ and y - 1 >= min_y and y + 1 <= max_y \ and screen[x - 1, y] == "#" and screen[x + 1, y] == "#" \ and screen[x, y - 1] == "#" and screen[x, y + 1] == "#": total_sum += x * y
from collections import defaultdict from int_code_solver import Solver with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) screen = defaultdict(int) solver = Solver(int_code_program) while not solver.is_finished: x = solver.solve() y = solver.solve() tile_id = solver.solve() screen[x, y] = tile_id print(sum(1 for tile_id in screen.values() if tile_id == 2))
import itertools from int_code_solver import Solver with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) solvers = [Solver(int_code_program.copy(), [i]) for i in range(50)] for solver in solvers: solver.solve() for i in itertools.cycle(range(50)): if not solvers[i].input_values: solvers[i].input_values = [-1] if destination := solvers[i].solve(): x = solvers[i].solve() y = solvers[i].solve() if destination == 255: print(y) break solvers[destination].input_values.extend([x, y])
from int_code_solver import Solver with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) int_code_program[0] = 2 # MAIN = A,A,B,C,A,C,B,C,A,B # A = L,4,L10,L,6 # B = L,6,L,4,R,8,R,8 # C = L,6,R,8,L,10,L,8,L,8 solution = list("""A,A,B,C,A,C,B,C,A,B L,4,L,10,L,6 L,6,L,4,R,8,R,8 L,6,R,8,L,10,L,8,L,8 n """) int_solution = [ord(c) for c in solution] solver = Solver(int_code_program, input_values=int_solution) while not solver.is_finished: output = solver.solve() if output and output <= 126: print(chr(output), end="") elif output: print(output)
from collections import defaultdict from int_code_solver import Solver with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) grid = defaultdict(int) for y in range(50): for x in range(50): solver = Solver(int_code_program, input_values=[x, y]) grid[x, y] = solver.solve() print(sum(grid.values())) max_y = max(y for x, y in grid) max_x = max(x for x, y in grid) for y in range(max_y + 1): for x in range(max_x + 1): if grid[x, y]: print("#", end="") else: print(" ", end="") print()
with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) todo = [((0, 0), [])] heapify(todo) discovered = defaultdict(lambda: -1) discovered[0, 0] = 1 while 2 not in discovered.values(): (x, y), path = heappop(todo) for direction, (x_diff, y_diff) in DIRECTIONS_MAP.items(): new_x = x + x_diff new_y = y + y_diff if (new_x, new_y) in discovered: continue solver = Solver(int_code_program.copy()) for d in path: result = solver.solve(d) result = solver.solve(direction) discovered[new_x, new_y] = result if result == 2: oxygen_location = new_x, new_y oxygen_path = path + [direction] break if result > 0: heappush(todo, ((new_x, new_y), path + [direction])) print_area(discovered, oxygen_path) print(len(oxygen_path))
from collections import defaultdict from int_code_solver import Solver with open("input.txt") as f: int_code_program = list(map(int, f.readline().strip().split(","))) int_code_program[0] = 2 screen = defaultdict(int) solver = Solver(int_code_program) x_paddle, x_ball = 0, 0 next_input = None while not solver.is_finished: x = solver.solve(next_input) y = solver.solve() tile_id = solver.solve() if x == -1 and y == 0: score = tile_id else: screen[x, y] = tile_id x_paddle = x if tile_id == 3 else x_paddle x_ball = x if tile_id == 4 else x_ball if x_ball > x_paddle: next_input = 1 elif x_ball < x_paddle: next_input = -1 else: next_input = 0 print(score)
def is_beam(x: int, y: int) -> bool: if (x, y) not in grid: solver = Solver(int_code_program, input_values=[x, y]) grid[x, y] = solver.solve() return grid[x, y] == 1