98 lines
2.2 KiB
Python
98 lines
2.2 KiB
Python
#!/bin/python3
|
|
import sys,re
|
|
from pprint import pprint
|
|
from functools import reduce
|
|
from operator import xor
|
|
|
|
input_f = 'input'
|
|
|
|
def list2int(x):
|
|
return list(map(int, x))
|
|
|
|
def toACSII(x):
|
|
for idx,i in enumerate(x):
|
|
x[idx] = ord(i)
|
|
return x
|
|
|
|
def XOR(x):
|
|
return reduce(xor, map(int, t))
|
|
|
|
part = 2
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
|
|
if part == 1:
|
|
|
|
size = 256
|
|
lengths = []
|
|
skip = 0
|
|
numbers = []
|
|
pos = 0
|
|
for i in range(0,size):
|
|
numbers.append(i)
|
|
|
|
with open(input_f) as file:
|
|
for line in file:
|
|
lengths = list2int(line.rsplit()[0].split(','))
|
|
|
|
for ldx, length in enumerate(lengths):
|
|
sub = [numbers[(pos + i) % len(numbers)] for i in range(length)]
|
|
rev = sub[::-1]
|
|
|
|
for i in range(length):
|
|
numbers[(pos + i) % len(numbers)] = rev[i]
|
|
|
|
pos += (length+skip)
|
|
pos = pos % len(numbers)
|
|
skip += 1
|
|
print(numbers[0]*numbers[1])
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
if part == 2:
|
|
|
|
size = 256
|
|
lengths = []
|
|
skip = 0
|
|
numbers = []
|
|
pos = 0
|
|
for i in range(0,size):
|
|
numbers.append(i)
|
|
|
|
|
|
with open(input_f) as file:
|
|
for line in file:
|
|
lengths = list(line.rsplit()[0].split()[0])
|
|
lengths = toACSII(lengths) + [17, 31, 73, 47, 23]
|
|
print(lengths)
|
|
|
|
for i in range(0,64):
|
|
for ldx, length in enumerate(lengths):
|
|
sub = [numbers[(pos + i) % len(numbers)] for i in range(length)]
|
|
rev = sub[::-1]
|
|
|
|
for i in range(length):
|
|
numbers[(pos + i) % len(numbers)] = rev[i]
|
|
|
|
pos += (length+skip)
|
|
pos = pos % len(numbers)
|
|
skip += 1
|
|
print(numbers)
|
|
|
|
dense = []
|
|
|
|
for i in range(0,16):
|
|
t = numbers[i*16:(i*16)+16]
|
|
dense.append(XOR(t))
|
|
|
|
|
|
for i in dense:
|
|
print(format(i, '02x'),end='')
|
|
print()
|