DEV Community

Discussion on: AoC Day 14: Chocolate Charts

Collapse
 
r0f1 profile image
Florian Rohrer

Python

n = 293801
# Part 1
q = [3,7]
e1, e2 = 0, 1
n_created = 0

while n_created < n+10:
    r1, r2 = q[e1], q[e2]
    for c in str(r1+r2):
        q.append(int(c))
        n_created += 1
    e1 = (e1 + 1 + r1) % len(q)
    e2 = (e2 + 1 + r2) % len(q)

print("".join(str(i) for i in q[n:n+10]))

# Part 2
ns = str(n)
def match(haystack):
    for nc, h in zip(reversed(ns), reversed(haystack)):
        if nc != str(h): return False
    return True

q = [3,7]
e1, e2 = 0, 1

while True:
    r1, r2 = q[e1], q[e2]
    for c in str(r1+r2):
        q.append(int(c))
        if match(q):
            print(len(q)-len(str(n)))
            break
    else:
        e1 = (e1 + 1 + r1) % len(q)
        e2 = (e2 + 1 + r2) % len(q)
        continue
    break