Files
2015
2016
2017
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
15.md
debug-full-aoc-response.html
solution.py
16
17
18
19
20
21
22
23
24
25
2018
2021
2022
2023
2024
__pycache__
.gitignore
LICENSE
README.md
fred.py
solution.py
AdventOfCode/2017/15/solution.py
2024-11-25 20:17:15 +01:00

73 lines
1.6 KiB
Python

#!/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)