AdventOfCode/2017/15/solution.py

73 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-11-25 20:17:15 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'test'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
if part == 1:
a = 634
b = 301
a_fak = 16807
b_fak = 48271
rem = 2147483647
count = 0
for i in range(0,40000000):
a = (a*a_fak)%rem
b = (b*b_fak)%rem
a_bin = bin(a)[2:].zfill(32)
b_bin = bin(b)[2:].zfill(32)
if a_bin[16:] == b_bin[16:]:
count += 1
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
a = 634
b = 301
a_fak = 16807
b_fak = 48271
rem = 2147483647
count = 0
a_matches = []
b_matches = []
while min(len(a_matches),len(b_matches)) < 5000000:
a = (a*a_fak)%rem
b = (b*b_fak)%rem
a_bin = bin(a)[2:].zfill(32)
b_bin = bin(b)[2:].zfill(32)
if a % 4 == 0:
a_matches.append(a_bin[16:])
if b % 8 == 0:
b_matches.append(b_bin[16:])
print(len(a_matches))
print(len(b_matches))
for x in range(0,min(len(a_matches),len(b_matches))):
if a_matches[x] == b_matches[x]:
count += 1
print(count)