-
-
Save mserrano/5a411856420cec55a16e179536adfcae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def zw(f, *args): | |
return [f(a) for a in zip(*args)] | |
def j(l, joiner=''): | |
return joiner.join(l) | |
def substrs(s, n): | |
ls = [s[i::n] for i in xrange(n)] | |
return zw(j, *ls) | |
def converge(f, e): | |
last = e | |
e = f(e) | |
while e != last: | |
last = e | |
e = f(e) | |
return e | |
def step(a): | |
b = a[::-1] | |
b = j('1' if x == '0' else '0' for x in b) | |
return a + '0' + b | |
def is_same(n): | |
if n[0] == n[1]: | |
return '1' | |
return '0' | |
def checksum_step(s): | |
if len(s) % 2 != 0: | |
return s | |
subs = substrs(s, 2) | |
return j(is_same(x) for x in subs) | |
def checksum(s): | |
return converge(checksum_step, s) | |
gen = '10001001100000001' | |
size = 272 | |
while len(gen) < size: | |
gen = step(gen) | |
g = gen[:size] | |
print 'part a', checksum(g) | |
size = 35651584 | |
while len(gen) < size: | |
gen = step(gen) | |
g = gen[:size] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment