-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday15_generators.py
86 lines (59 loc) · 2.65 KB
/
day15_generators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Advent of Code Day 15
http://adventofcode.com/2017/day/15"""
from typing import Generator
def next_num_in_sequence(seed: int, factor: int, divide_by=1) -> int:
last_num = seed
while True:
value = (last_num * factor) % 2_147_483_647
if value % divide_by == 0:
yield value
last_num = value
# test next_num_in_sequence
test_generator_A = next_num_in_sequence(65, 16_807)
test_generator_B = next_num_in_sequence(8_921, 48_271)
test_result_A = []
test_result_B = []
for _ in range(5):
test_result_A.append(next(test_generator_A))
test_result_B.append(next(test_generator_B))
assert test_result_A == [1092455, 1181022009, 245556042, 1744312007, 1352636452]
assert test_result_B == [430625591, 1233683848, 1431495498, 137874439, 285222916]
def match_lowest_16_bits(num: int, other_num: int) -> bool:
if bin(num)[-16:] == bin(other_num)[-16:]:
return True
return False
# test new generator
test_genA = next_num_in_sequence(65, 16_807, divide_by=4)
test_genB = next_num_in_sequence(8_921, 48_271, divide_by=8)
test_result_A = []
test_result_B = []
for _ in range(5):
test_result_A.append(next(test_genA))
test_result_B.append(next(test_genB))
assert test_result_A == [1352636452, 1992081072, 530830436, 1980017072, 740335192]
assert test_result_B == [1233683848, 862516352, 1159784568, 1616057672, 412269392]
is_match = []
for num, other_num in zip(test_result_A, test_result_B):
is_match.append(match_lowest_16_bits(num, other_num))
assert is_match == [False, False, False, False, False]
def count_matching_pairs(upper_bound: int,
genA: Generator,
genB: Generator) -> bool:
judge_count = 0
for _ in range(upper_bound):
if match_lowest_16_bits(next(genA), next(genB)):
judge_count += 1
return judge_count
if __name__ == '__main__':
test_generator_A = next_num_in_sequence(65, 16_807)
test_generator_B = next_num_in_sequence(8_921, 48_271)
# print(count_matching_pairs(40_000_000, test_generator_A, test_generator_B))
generator_A = next_num_in_sequence(618, 16_807)
generator_B = next_num_in_sequence(814, 48_271)
# print(count_matching_pairs(40_000_000, generator_A, generator_B))
test_generator_A = next_num_in_sequence(65, 16_807, divide_by=4)
test_generator_B = next_num_in_sequence(8_921, 48_271, divide_by=8)
# print(count_matching_pairs(5_000_000, test_generator_A, test_generator_B))
generator_A = next_num_in_sequence(618, 16_807, divide_by=4)
generator_B = next_num_in_sequence(814, 48_271, divide_by=8)
print(count_matching_pairs(5_000_000, generator_A, generator_B))