AdventOfCode/2017/10/solution.py

99 lines
2.2 KiB
Python
Raw Normal View History

2024-11-23 10:02:58 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
2024-11-23 13:24:54 +01:00
from functools import reduce
from operator import xor
2024-11-23 10:02:58 +01:00
2024-11-25 17:42:14 +01:00
input_f = 'test3'
2024-11-23 10:02:58 +01:00
def list2int(x):
return list(map(int, x))
2024-11-23 13:24:54 +01:00
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
2024-11-23 10:02:58 +01:00
#########################################
# #
# 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:
2024-11-23 13:24:54 +01:00
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))
2024-11-25 17:42:14 +01:00
print(dense)
2024-11-23 13:24:54 +01:00
for i in dense:
print(format(i, '02x'),end='')
print()