diff --git a/15_1.rb b/15_1.rb new file mode 100644 index 0000000..af71bd8 --- /dev/null +++ b/15_1.rb @@ -0,0 +1,30 @@ +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 + diff --git a/15_2.rb b/15_2.rb new file mode 100644 index 0000000..cfc0b19 --- /dev/null +++ b/15_2.rb @@ -0,0 +1,40 @@ +GEN_A_START = 679 +GEN_B_START = 771 +GEN_A_TEST_S = 65 +GEN_B_TEST_S = 8921 +GEN_A_P = 4 +GEN_B_P = 8 + +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 + +def find_next val, fac, mul + while(true) do + val = (val * fac) % MOD + break if val % mul == 0 + end + val +end + +a_rem = GEN_A_START +b_rem = GEN_B_START + +for i in 1..(5*10**6) do + if i % 1000000 == 0 then puts i end + a_rem = find_next a_rem, GEN_A_FAC, GEN_A_P + b_rem = find_next b_rem, GEN_B_FAC, GEN_B_P + if compare a_rem, b_rem then matches +=1 end +end + +puts matches +