AdventOfCode/15_1.rb

31 lines
484 B
Ruby
Raw Permalink Normal View History

GEN_A_START = 679
GEN_B_START = 771
GEN_A_TEST_S = 65
GEN_B_TEST_S = 8921
GEN_A_FAC = 16807
GEN_B_FAC = 48271
MOD = 2147483647
def compare a, b
a_bin = a.to_s(2)[-16..-1]
b_bin = b.to_s(2)[-16..-1]
a_bin == b_bin
end
matches = 0
a_rem = GEN_A_START
b_rem = GEN_B_START
for i in 1..(40*10**6) do
if i % 1000000 == 0 then puts i end
a_rem = (a_rem * GEN_A_FAC) % MOD
b_rem = (b_rem * GEN_B_FAC) % MOD
if compare a_rem,b_rem then matches += 1 end
end
puts matches