def p2(): seen = [] for x in read_lines_array(16, ','): operations = x data = 'abcdefghijklmnop' for i in xrange(1000000000): if data in seen: print(seen[1000000000 % i]) break seen.append(data) for op in operations: if op[0] == 's': s = int(op[1:]) data = data[-s:] + data[:-s] elif op[0] == 'x': f, t = op[1:].split('/') f, t = int(f), int(t) data = swap(data, f, t) elif op[0] == 'p': fn, tn = op[1:].split('/') f = data.index(fn) t = data.index(tn) data = swap(data, f, t)
def p2(): instructions = [line for line in read_lines_array(18)] registries = [{'p': 0}, {'p': 1}] queues = [[], []] idx = [0, 0] stop = [False, False] send = [0, 0] l = len(instructions) register_index = 0 while True: i = idx[register_index] instruction = instructions[i] new_stop, new_idx = do_operation_2(register_index, 1 - register_index, i, instructions, registries, instruction, queues, send) stop[register_index] = new_stop idx[register_index] = new_idx if new_stop: if len(queues[0]) == 0 and len(queues[1]) == 0: break print register_index, instruction print registries[0] print registries[1] print print '---------------------', send, len(queues[0]), len(queues[1]) register_index = 1 - register_index print send, len(queues[0]), len(queues[1])
def p1(): instructions = [line for line in read_lines_array(23)] registries = { 'a': 1, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, } queues = [[], []] idx = 0 stop = [False, False] send = [ 0, ] l = len(instructions) register_index = 0 a = 0 while True: if a % 10000 == 0: print a, registries stop, idx = do_operation(idx, instructions, registries, send) if stop: break a += 1
def p1(): instructions = [line for line in read_lines_array(18)] registries = [ {}, ] last_played_frequency = 0 idx = 0 stop = False l = len(instructions) while not stop or idx < l: instruction = instructions[idx] last_played_frequency, stop, idx = do_operation( idx, instructions, registries, instruction, last_played_frequency) if stop: break idx += 1 print last_played_frequency
def p1(): total = 0 for data in read_lines_array(2): mi = None ma = None for i in data: m = int(i) if mi is None: mi = m elif m < mi: mi = m if ma is None: ma = m elif m > ma: ma = m total += (ma - mi) print total
def p1(): for x in read_lines_array(16, ','): operations = x data = 'abcdefghijklmnop' for op in operations: if op[0] == 's': s = int(op[1:]) data = data[-s:] + data[:-s] elif op[0] == 'x': f, t = op[1:].split('/') f, t = int(f), int(t) data = swap(data, f, t) elif op[0] == 'p': fn, tn = op[1:].split('/') f = data.index(fn) t = data.index(tn) data = swap(data, f, t) print data
def p1(): valid = 0 for d in read_lines_array(4): valid += 1 if (len(set(d)) == len(d)) else 0 print valid
def p2(): valid = 0 for d in read_lines_array(4): valid += 1 if (len(set([''.join(sorted(x)) for x in d])) == len(d)) else 0 print valid