2019 day 16 and 17 wip

This commit is contained in:
2024-08-18 19:38:13 -04:00
parent ebf65905b4
commit e9f5542be8
3 changed files with 114 additions and 14 deletions

View File

@@ -7,10 +7,7 @@ def part_1_with_numpy(data):
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)
]
xs = [pattern[((i + 1) // (round + 1)) % len(pattern)] for i in range(n)]
xss.append(xs)
return np.array(xss)
@@ -32,7 +29,7 @@ def phase(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])
out += pattern[pattern_i] * digits_in[i]
i += 1
out = abs(out) % 10
digits_out.append(out)
@@ -53,10 +50,12 @@ def phase_with_offset(digits_in, pattern, offset):
pattern_i = ((i + 1) // (round + 1)) % len(pattern)
pattern_value = pattern[pattern_i]
if pattern_value != 0:
out += pattern_value * sum(digits_in[i : min(i + 1 + round, len(digits_in))])
out += pattern_value * sum(
digits_in[i : min(i + 1 + round, len(digits_in))]
)
# for j in range(i, min(i + 1 + round, len(digits_in))):
# out += (pattern_value * digits_in[j])
i += (round + 1)
i += round + 1
out = abs(out) % 10
digits_out[round] = out
@@ -88,25 +87,26 @@ def part_1(data):
# 2. The solution is not crazy. It will be rather obvious.
# 3. 6_500_000 * 6_500_000 is definitely too much to brute force.
# 4. Can we go from O(N^2) to O(N) somehow? Yes, that's what we have to do.
# The whole point of FFT is to get from O(N^2) to O(N*log(N)). Now,
# The whole point of FFT is to get from O(N^2) to O(N*log(N)). Now,
# how exactly do we do that?
#
#
# Ways to improve performance:
#
#
# 1. Speed up `phase` significantly. Yes, but how?
# 2. Only compute a subset of the lists? - No!
# 3. Discover some kind of pattern? - No!
#
# Assumptions:
#
#
# 1. I need every digit of the previous round. - False!
# 2. I cannot just operate on a subset. - False!
#
#
# Non-approaches:
#
#
# 1. Fancy recursive algorithm that selectively picks fields.
# 2. Pattern detection or subset consideration.
def main():
data = get_data(__file__)
# part_1_with_numpy(data)