-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp16.py
44 lines (33 loc) · 1.2 KB
/
p16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from itertools import cycle
from aocd import data
PHASES = 100
PATTERN = (0, 1, 0, -1)
signal = [int(digit) for digit in data]
for phase in range(PHASES):
new_signal = ''
for position in range(1, len(data)+1):
pattern = cycle([i for i in PATTERN for _ in range(position)])
next(pattern)
digit = 0
for element, value in zip(signal, pattern):
digit += element*value
new_signal += str(digit)[-1]
signal = [int(digit) for digit in new_signal]
print('Part 1:', ''.join(str(digit) for digit in signal[:8]))
signal = [int(digit) for digit in data] * 10_000
offset = int(data[:7])
signal = signal[offset:]
for phase in range(PHASES):
# every digit before offset is
# multiplied by zero
partial_sum = sum(signal)
for position in range(len(signal)):
# store value to remove from sum
# as we're about to change it
old_value = signal[position]
signal[position] = partial_sum % 10
# current digit will be multiplied by zero
# in next theoretical part of the pattern
# so we just remove it from the sum
partial_sum -= old_value
print('Part 2:', ''.join(str(digit) for digit in signal[:8]))