Skip to content

Instantly share code, notes, and snippets.

@erikaderstedt
Created December 16, 2019 07:25
Show Gist options
  • Save erikaderstedt/ab835717f56705830bed23a6997c23db to your computer and use it in GitHub Desktop.
Save erikaderstedt/ab835717f56705830bed23a6997c23db to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import numpy as np
f = open('16.in').read().strip()
l = np.array([int(s) for s in f], dtype=int)
def get_matrix(llen):
r = np.zeros((llen, llen), dtype=int)
base_pattern = np.array([0,1,0,-1],dtype=int)
nrep = 1
for i in range(llen):
b = np.transpose(np.tile(base_pattern, (nrep,1))).flatten()
c = len(l)//len(b) + 1
b = np.tile(b, c)
r[i] = b[1:(llen+1)]
nrep = nrep + 1
return r
m = get_matrix(len(l))
for i in range(100):
l = np.abs(np.dot(m, l)) % 10
print("Pt 1:",''.join(l[:8].astype(str)))
l = np.array([int(s) for s in f]*10000, dtype=int)
offset = int(f[:7])
# print(offset / len(l), len(l), offset)
# 7 digits -> ~1 M. Input length 6.5 M. At least for my case I'm at 5971269.
# At this point the tail end of the list is all multiplied by 1's.
# The nth element from the back is the cumulative sum of elements from the end back to that point.
# This seems to hold for all the examples as well, probably not a coincidence.
l2 = l[offset:][::-1]
for i in range(100):
l2 = l2.cumsum() % 10
print("Pt 2:",''.join((l2[-8:][::-1]).astype(str)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment