advent-of-code/2017-python/solutions/day_15.py

43 lines
1.1 KiB
Python
Raw Normal View History

2021-11-01 16:40:46 +01:00
from solutions import BaseSolution
class Solution(BaseSolution):
2025-05-06 20:14:58 +02:00
input_file = "15.txt"
2021-11-01 16:40:46 +01:00
def __str__(self):
2025-05-06 20:14:58 +02:00
return "Day 15: Dueling Generators"
2021-11-01 16:40:46 +01:00
def _calc(self, x, f, m=1):
x = (x * f) % 2147483647
if x % m != 0:
return self._calc(x, f, m)
else:
return x
def solve(self, puzzle_input):
af, bf = (16807, 48271)
a, b = [int(pi.split()[-1]) for pi in puzzle_input.splitlines()]
j = 0
for _ in range(40 * 10**6):
a = self._calc(a, af)
b = self._calc(b, bf)
2025-05-06 20:14:58 +02:00
if "{:b}".format(a)[-16:] == "{:b}".format(b)[-16:]:
2021-11-01 16:40:46 +01:00
j += 1
return j
def solve_again(self, puzzle_input):
af, bf = (16807, 48271)
a, b = [int(pi.split()[-1]) for pi in puzzle_input.splitlines()]
j = 0
2025-05-06 20:14:58 +02:00
for _ in range(5 * 10**6):
2021-11-01 16:40:46 +01:00
a = self._calc(a, af, 4)
b = self._calc(b, bf, 8)
2025-05-06 20:14:58 +02:00
if "{:b}".format(a)[-16:] == "{:b}".format(b)[-16:]:
2021-11-01 16:40:46 +01:00
j += 1
return j
2025-05-06 20:14:58 +02:00
if __name__ == "__main__":
2021-11-01 16:40:46 +01:00
solution = Solution()
solution.show_results()