|
4 | 4 | test_data = {}
|
5 | 5 |
|
6 | 6 | test = 1
|
7 |
| -test_data[test] = {"input": """Generator A starts with 65 |
| 7 | +test_data[test] = { |
| 8 | + "input": """Generator A starts with 65 |
8 | 9 | Generator B starts with 8921""",
|
9 |
| - "expected": ['588', 'Unknown'], |
10 |
| - } |
11 |
| - |
12 |
| -test = 'real' |
13 |
| -input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
14 |
| -test_data[test] = {"input": open(input_file, "r+").read().strip(), |
15 |
| - "expected": ['597', '303'], |
16 |
| - } |
| 10 | + "expected": ["588", "Unknown"], |
| 11 | +} |
| 12 | + |
| 13 | +test = "real" |
| 14 | +input_file = os.path.join( |
| 15 | + os.path.dirname(__file__), |
| 16 | + "Inputs", |
| 17 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 18 | +) |
| 19 | +test_data[test] = { |
| 20 | + "input": open(input_file, "r+").read().strip(), |
| 21 | + "expected": ["597", "303"], |
| 22 | +} |
17 | 23 |
|
18 | 24 | # -------------------------------- Control program execution -------------------------------- #
|
19 | 25 |
|
20 |
| -case_to_test = 'real' |
21 |
| -part_to_test = 2 |
| 26 | +case_to_test = "real" |
| 27 | +part_to_test = 2 |
22 | 28 | verbose_level = 1
|
23 | 29 |
|
24 | 30 | # -------------------------------- Initialize some variables -------------------------------- #
|
25 | 31 |
|
26 |
| -puzzle_input = test_data[case_to_test]['input'] |
27 |
| -puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
28 |
| -puzzle_actual_result = 'Unknown' |
| 32 | +puzzle_input = test_data[case_to_test]["input"] |
| 33 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 34 | +puzzle_actual_result = "Unknown" |
29 | 35 |
|
30 | 36 |
|
31 | 37 | # -------------------------------- Actual code execution -------------------------------- #
|
32 | 38 |
|
33 | 39 | divisor = 2147483647
|
34 |
| -factors = {'A': 16807, 'B': 48271} |
35 |
| -value = {'A': 0, 'B': 0} |
| 40 | +factors = {"A": 16807, "B": 48271} |
| 41 | +value = {"A": 0, "B": 0} |
36 | 42 |
|
37 | 43 |
|
38 |
| -def gen_a (): |
| 44 | +def gen_a(): |
| 45 | + x = value["A"] |
39 | 46 | while True:
|
40 |
| - value['A'] *= factors['A'] |
41 |
| - value['A'] %= divisor |
42 |
| - if value['A'] % 4 == 0: |
43 |
| - yield value['A'] |
| 47 | + x *= 16807 |
| 48 | + x %= 2147483647 |
| 49 | + if x % 4 == 0: |
| 50 | + yield x |
| 51 | + |
44 | 52 |
|
45 |
| -def gen_b (): |
| 53 | +def gen_b(): |
| 54 | + x = value["B"] |
46 | 55 | while True:
|
47 |
| - value['B'] *= factors['B'] |
48 |
| - value['B'] %= divisor |
49 |
| - if value['B'] % 8 == 0: |
50 |
| - yield value['B'] |
| 56 | + x *= 48271 |
| 57 | + x %= 2147483647 |
| 58 | + if x % 8 == 0: |
| 59 | + yield x |
| 60 | + |
51 | 61 |
|
52 | 62 | if part_to_test == 1:
|
53 |
| - for string in puzzle_input.split('\n'): |
| 63 | + for string in puzzle_input.split("\n"): |
54 | 64 | _, generator, _, _, start_value = string.split()
|
55 | 65 | value[generator] = int(start_value)
|
56 | 66 |
|
57 | 67 | nb_matches = 0
|
58 |
| - for i in range (40 * 10 ** 6): |
| 68 | + for i in range(40 * 10 ** 6): |
59 | 69 | value = {gen: value[gen] * factors[gen] % divisor for gen in value}
|
60 |
| - if '{0:b}'.format(value['A'])[-16:] == '{0:b}'.format(value['B'])[-16:]: |
| 70 | + if "{0:b}".format(value["A"])[-16:] == "{0:b}".format(value["B"])[-16:]: |
61 | 71 | nb_matches += 1
|
62 | 72 |
|
63 | 73 | puzzle_actual_result = nb_matches
|
64 | 74 |
|
65 | 75 |
|
66 | 76 | else:
|
67 |
| - for string in puzzle_input.split('\n'): |
| 77 | + for string in puzzle_input.split("\n"): |
68 | 78 | _, generator, _, _, start_value = string.split()
|
69 | 79 | value[generator] = int(start_value)
|
70 | 80 |
|
71 | 81 | nb_matches = 0
|
72 | 82 | A = gen_a()
|
73 | 83 | B = gen_b()
|
74 |
| - for count_pairs in range (5 * 10**6): |
| 84 | + for count_pairs in range(5 * 10 ** 6): |
75 | 85 | a, b = next(A), next(B)
|
76 |
| - if '{0:b}'.format(a)[-16:] == '{0:b}'.format(b)[-16:]: |
| 86 | + if a & 0xFFFF == b & 0xFFFF: |
77 | 87 | nb_matches += 1
|
78 | 88 |
|
79 |
| - |
80 | 89 | puzzle_actual_result = nb_matches
|
81 | 90 |
|
82 | 91 |
|
83 |
| - |
84 | 92 | # -------------------------------- Outputs / results -------------------------------- #
|
85 | 93 |
|
86 | 94 | if verbose_level >= 3:
|
87 |
| - print ('Input : ' + puzzle_input) |
88 |
| -print ('Expected result : ' + str(puzzle_expected_result)) |
89 |
| -print ('Actual result : ' + str(puzzle_actual_result)) |
90 |
| - |
91 |
| - |
92 |
| - |
93 |
| - |
| 95 | + print("Input : " + puzzle_input) |
| 96 | +print("Expected result : " + str(puzzle_expected_result)) |
| 97 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments