2019 day 16 in-progress

This commit is contained in:
felixm 2024-08-17 16:01:06 -04:00
parent f88eb14d24
commit aba7ad5bde
2 changed files with 102 additions and 1 deletions

View File

@ -1,5 +1,4 @@
from lib import get_data, str_to_ints, add2
from collections import deque
from d9 import Amp

102
2019/d16.py Normal file
View File

@ -0,0 +1,102 @@
from lib import get_data
import numpy as np
def phase(digits_in, pattern):
digits_out = []
for round in range(len(digits_in)):
i, out = 0, 0
while i < len(digits_in):
pattern_i = ((i + 1) // (round + 1)) % len(pattern)
out += (pattern[pattern_i] * digits_in[i])
i += 1
out = abs(out) % 10
digits_out.append(out)
return digits_out
def make_base_matrix(pattern, n):
xss = []
for round in range(n):
xs = [
pattern[((i + 1) // (round + 1)) % len(pattern)]
for i in range(n)
]
xss.append(xs)
return np.array(xss)
# for round in range(len(digits_in)):
# i, out = 0, 0
# while i < len(digits_in):
# pattern_i = ((i + 1) // (round + 1)) % len(pattern)
def phase(matrix, vector):
return matrix * vector
def part_1(data):
pattern = [0, 1, 0, -1]
input = int(data.strip())
v = np.array(list(map(int, str(input))))
m = make_base_matrix(pattern, len(v))
func = np.vectorize(lambda x: abs(x) % 10)
for _ in range(100):
v = func(np.dot(m, v))
print("".join(map(str, v[:8].tolist())))
input = int(data.strip())
# input = 12345678
v = np.array(list(map(int, str(input))) * 10000)
print(len(v), v.shape)
return
m = make_base_matrix(pattern, len(v))
print(m.shape)
func = np.vectorize(lambda x: abs(x) % 10)
for i in range(100):
print(i)
v = func(np.dot(m, v))
print("".join(map(str, v[:8].tolist())))
return
for _ in range(100):
input = phase(input, pattern)
print("".join(map(str, input[:8])))
input = int(data.strip())
input = list(map(int, str(input)))
offset = int("".join(map(str, input[:7])))
print(len(input))
input = input * 10_000
print(len(input))
for i in range(100):
print(i)
input = phase(input, pattern)
r = []
for i in range(offset, offset + 8):
r.append(input[i])
print("".join(map(str, r)))
# Ways to improve performance:
#
# 1. Speed up `phase` significantly.
# 2. Only compute a subset of the lists?
# 3. Discover some kind of pattern?
#
# Assumptions:
#
# 1. I need every digit of the previous round.
# 2. I cannot just operate on a subset.
# 3.
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()