def dec7_star1(): #prgdata = "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0" #prgdata = "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0" #prgdata = "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0" seq = [0, 1, 2, 3, 4] perms = itertools.permutations(seq) maxres = None for p in perms: A = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[0], 0], name="A").execute() B = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[1], A.result], name="B").execute() C = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[2], B.result], name="C").execute() D = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[3], C.result], name="D").execute() E = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[4], D.result], name="E").execute() if not maxres or E.result > maxres: maxres = E.result setting = p print(f"Max: {maxres}/{setting}")
def __init__(self, intcode=None): prg = self.get_prg() if not intcode is None: prg[0] = intcode self.c = intcomputer(prg, param_array=[]) self.grid = [] self.alignments = []
def dec13_star1(): curdir = os.path.dirname(os.path.abspath(__file__)) filename = f'{curdir}\\dec13.txt' prgdata = [int(_) for _ in open(filename, 'r').readline().split(",")] c = intcomputer(prgdata, param_array=[]).execute() blocktiles = len([_ for _ in c.outputarray[2::3] if _ == 2]) print("Answer: ", blocktiles)
def dec9_star2(): filename = f'{curdir}\\dec9.txt' prgdata = open(filename, 'r').readline() prg = [int(_) for _ in prgdata.split(",")] computer = intcomputer(prg, input=[2]).execute() # print([int(_) for _ in prgdata.split(",")]) print(computer.outputarray)
def dec11_star1(): global cnt filename = f'{curdir}\\dec11.txt' prgdata = open(filename, 'r').readline() prg = [int(_) for _ in prgdata.split(",")] computer = intcomputer(prg, name=f"PainterRobot{cnt}") cnt += 1 robot = Robot(computer) robot.run() #print(len(robot.grid)) print("DONE")
def dec7_star2(): #prgdata = "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5" #prgdata = "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10" seq = [5, 6, 7, 8, 9] perms = itertools.permutations(seq) maxres = None for p in perms: A = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[0]], name="A") B = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[1]], name="B") C = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[2]], name="C") D = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[3]], name="D") E = intcomputer([int(_) for _ in prgdata.split(",")], input=[p[4]], name="E") computers = [A, B, C, D, E] c = 0 while not any([_._terminated for _ in computers]): computers[c].add_input(computers[(c - 1 + 5) % 5].result) computers[c].execute() c = (c + 1) % 5 print(p) if not maxres or E.result > maxres: maxres = E.result setting = p print(f"Max: {maxres}/{setting}")
def dec11_star2(): filename = f'{curdir}\\dec11.txt' prgdata = open(filename, 'r').readline() prg = [int(_) for _ in prgdata.split(",")] startgrid = defaultdict(int) startgrid[Cell(0, 0)] = 1 computer = intcomputer(prg, name=f"PainterRobot2") robot = Robot(computer, grid=startgrid) robot.run() robot.printtrack() print(len(robot.grid)) print("DONE")
def __init__(self): self.DIRECTION = [1, 4, 2, 3] # N E S W self.MOVES = { 1: Cell(0, 1), # N 4: Cell(1, 0), # E 2: Cell(0, -1), # S 3: Cell(-1, 0) } # W self.current_cell = Cell(0, 0) self.current_direction = 0 # self.trail = defaultdict(list) ## self.trail[self.current_cell] = [self.current_direction] self.trail = Myqueue() self.grid = {} self.grid[self.current_cell] = "." prgdata = self.get_prg() self.c = intcomputer(prgdata, param_array=[])
def dec13_star2(): curdir = os.path.dirname(os.path.abspath(__file__)) filename = f'{curdir}\\dec13.txt' prgdata = [int(_) for _ in open(filename, 'r').readline().split(",")] prgdata[0] = 2 scorecell = Cell(-1, 0) c = intcomputer(prgdata, param_array=[]) grid = make_grid(c.execute().outputarray) while blockcount(grid) > 0 and not c._terminated: ball = get_ball_pos(grid) paddle = get_paddle_pos(grid) c.add_input(move_paddle(ball, paddle)) if scorecell in grid: print(f"score: {grid[scorecell]}") grid = make_grid(c.execute().outputarray) print_grid(grid) print(grid[Cell(-1, 0)])
def dec9_star1(): prgdata = "109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99" prgdata = "1102,34915192,34915192,7,4,7,99,0" prgdata = "104,1125899906842624,99" filename = f'{curdir}\\dec9.txt' prgdata = open(filename, 'r').readline() #prgdata = "03,0,99" #prgdata = "109,100,203,5,99" #prgdata = "1005,2,5,04,1,99" #prgdata = "2002,4,10,0,99" #prgdata = "109,3,2207,2,4,7,99,04,6,99" #prgdata = "109,3,209,4,2201,-2,-3,1,99" #prgdata = "109,1,203,1,203,1,99" #prgdata = "109,1,2208,3,4,0,99,99" #prgdata = "003,3,99,3" #prgdata = "103,3,99,3" #prgdata = "109,2,203,3,99,3" prg = [int(_) for _ in prgdata.split(",")] computer = intcomputer(prg, input=[1]).execute() print([int(_) for _ in prgdata.split(",")]) print(computer.prog) print(computer.outputarray)
def get_beam(self, x, y): return intcomputer(self.prg.copy(), param_array=[x, y], debug=False).execute().pop_output()
def dec5_star2(): filename = f'{curdir}\\dec5.txt' prgdata = open(filename, 'r').readline() prg = [int(_) for _ in prgdata.split(",")] c = intcomputer(prg, input=[5]).execute() print(f"Result: {c.result}")
def dec5_star1(): filename = f'{curdir}\\dec5.txt' prgdata = open(filename, 'r').readline() prg = [int(_) for _ in prgdata.split(",")] intcomputer(prg, input=[1]).execute()
def runprog(prg, noun, verb): prg[1] = noun prg[2] = verb return intcomputer(prg).execute().prog[0]