#!/usr/bin/env python from operator import xor, concat from functools import reduce from itertools import repeat isPart1 = False my_input = "230,1,2,221,97,252,168,169,57,99,0,254,181,255,235,167" my_input = [ord(item) for item in my_input] my_input += [17, 31, 73, 47, 23] if isPart1 == True: my_input = [230,1,2,221,97,252,168,169,57,99,0,254,181,255,235,167] my_list = range(0,256) my_list_length = len(my_list) current_position = 0 skip_size = 0 if isPart1 == True: num_of_rounds = 1 else: num_of_rounds = 64 for i in repeat(None, num_of_rounds): for length in my_input: end_position = current_position + length reversed_list = [] if end_position > my_list_length: end_position = end_position % my_list_length reversed_list = my_list[current_position:] + my_list[0:end_position] ##print("Selected-part: " + str(reversed_list)) reversed_list.reverse() ##print("Reversed-part: " + str(reversed_list)) ##raw_input() my_list = reversed_list[(my_list_length-current_position):] + my_list[end_position:current_position] + reversed_list[0:(my_list_length-current_position)] else: reversed_list = my_list[current_position:end_position] ##print("Selected-part: " + str(reversed_list)) reversed_list.reverse() ##print("Reversed-part: " + str(reversed_list)) ##raw_input() my_list = my_list[0:current_position] + reversed_list + my_list[end_position:] current_position = (current_position + length + skip_size) % my_list_length skip_size += 1 ##print(my_list) ##raw_input() dense_hash = [] for i in range(0,16): dense_hash.append(reduce(xor, my_list[i*16:(i+1)*16])) dense_hash = [(hex(item)[2:] if len(hex(item)[2:])==2 else '0'+hex(item)[2:]) for item in dense_hash] hex_hash = ''.join(dense_hash) ##print(my_list) if isPart1 == True: print("Part 1: " + str(my_list[0] * my_list[1])) else: print("Part 2: " + hex_hash)