llist[input_seq[i]] = input_seq[i + 1] llist[input_seq[-1]] = input_seq[0] current_cup = input_seq[0] # pprint(llist) for i in range(move_count): llist, current_cup = llist_move(llist, current_cup, 1, input_max) a = llist[1] b = llist[a] return a, b, a * b """ Use script args to execute the right function. """ if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == '1': res = solve1((Input(DAY).read())) print(res) if len(sys.argv) > 1 and sys.argv[1] == '1t': res = solve1((test_input(DAY).read())) print(res) if len(sys.argv) > 1 and sys.argv[1] == '2': res = solve2((Input(DAY).read())) print(res) if len(sys.argv) > 1 and sys.argv[1] == '2t': res = solve2((test_input(DAY).read())) print(res)
if (a + b) == 2020: return a * b raise Exception("Sorry, no match found in your input") """ PART 2 """ def solve2(input): for i in range(len(input)): for j in range(i + 1, len(input)): for k in range(j + 1, len(input)): a = int(input[i]) b = int(input[j]) c = int(input[k]) if (a + b + c) == 2020: return a * b * c raise Exception("Sorry, no match found in your input") if __name__ == '__main__': if sys.argv[1] == '1': res = solve1((Input(DAY).readlines())) print(res) if sys.argv[1] == '2': res = solve2((Input(DAY).readlines())) print(res)
# Python 3.x from aoc_utilities import Input DAY = 18 import collections import multiprocessing.pool PROGRAM = Input(DAY).readlines() def run(ident, inqueue, outqueue): regs = collections.defaultdict(int) regs['p'] = ident def val(v): try: return int(v) except ValueError: return regs[v] pc = 0 count = 0 played = None while 0 <= pc < len(PROGRAM) - 1: cmd = PROGRAM[pc].split() if cmd[0] == 'snd': played = val(cmd[1]) if outqueue:
self.blocked = True return elif ins[0] == 'jgz': if self.get(ins[1]) > 0: self.ip += self.get(ins[2]) return self.ip += 1 def get(self, v): try: return int(v) except ValueError: return self.regs[v] def solve_p2(instr): p0 = Program(0, None, instr) p1 = Program(1, p0, instr) p0.other = p1 while not ((p0.terminated or p0.blocked) and (p1.terminated or p1.blocked)): p0.next() p1.next() return p1.sent res = solve_p2(Input(DAY).readlines()) print(res)
i += 1 elif op == 'mod': registers[inst[1]] %= get(registers, inst[2]) i += 1 elif op == 'rcv': if get(registers, inst[1]) != 0: registers['recovered'] = registers['sound'] print("first sound recovered !") return registers['recovered'] i += 1 elif op == 'jgz': if get(registers, inst[1]) > 0: i += get(registers, inst[2]) else: i += 1 else: print("unrecognized operator : {}".format(op)) return None # assert(solve1(teststr.splitlines()) == XXXXX) res = solve1(Input(DAY).readlines()) print(res) # def solve2(input): # pass # # res = solve2(parse_words(Input(DAY).readline())) # print(res)