Skip to content

Instantly share code, notes, and snippets.

@PamelaM
Created December 15, 2017 14:23
Show Gist options
  • Save PamelaM/d3be83c12927f40e9b5cf5245c9271a5 to your computer and use it in GitHub Desktop.
Save PamelaM/d3be83c12927f40e9b5cf5245c9271a5 to your computer and use it in GitHub Desktop.
Advent of Code 2017, Day 15
import time
def generator(input, factor, criteria):
value = input
ignored = 0
while True:
value = (value * factor) % 2147483647
if (value % criteria) == 0:
yield value, ignored
else:
ignored += 1
def run(input_a, input_b, crit_a, crit_b, num_loops):
matches = 0
gen_a = generator(input_a, 16807, crit_a)
gen_b = generator(input_b, 48271, crit_b)
for idx in xrange(num_loops):
val_a, ignored_a = gen_a.next()
val_b, ignored_b = gen_b.next()
if 0:
print "{:18} {:18}".format(val_a, val_b)
print "{:18} {:18}".format(ignored_a, ignored_b)
print "{:032b} {:016b}".format(val_a, val_a & 0xFFFF)
print "{:032b} {:016b} {}".format(val_b, val_b & 0xFFFF, (val_a & 0xFFFF) == (val_b & 0xFFFF))
if (val_a & 0xFFFF) == (val_b & 0xFFFF):
matches += 1
return matches
start = time.time();print run(65, 8921, 1, 1, 5), "%.5f" % (time.time()-start,)
start = time.time();print run(65, 8921, 1, 1, 40000000), "%.5f" % (time.time()-start,)
start = time.time();print run(703, 516, 1, 1, 40000000), "%.5f" % (time.time()-start,)
start = time.time();print run(65, 8921, 4, 8, 5), "%.5f" % (time.time()-start,)
start = time.time();print run(65, 8921, 4, 8, 5000000), "%.5f" % (time.time()-start,)
start = time.time();print run(703, 516, 4, 8, 5000000), "%.5f" % (time.time()-start,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment