-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_dueling_generators.rb
More file actions
49 lines (40 loc) · 908 Bytes
/
15_dueling_generators.rb
File metadata and controls
49 lines (40 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
MOD = 2 ** 31 - 1
A, B = if ARGV.size >= 2 && ARGV.all? { |arg| arg.match?(/^\d+$/) }
ARGV
else
ARGF.read.scan(/\d+/)
end.map(&method(:Integer))
AM = 16807
BM = 48271
c_lib = File.join(__dir__, 'c', 'lib15.so')
if File.exist?(c_lib)
require 'fiddle'
lib = Fiddle.dlopen(c_lib)
['part1', 'part2'].map { |f| Fiddle::Function.new(
lib[f], [Fiddle::TYPE_LONG_LONG] * 2, Fiddle::TYPE_LONG_LONG,
)}.each { |f| puts f.call(A, B) }
exit 0
end
# Unfortunately, I will have to use c = 0, c += 1, puts c
# rather than puts N.times.count {},
# it brings from 10.5 seconds to 9.5.
a = A
b = B
c = 0
40_000_000.times {
a = a * AM % MOD
b = b * BM % MOD
c += 1 if a & 0xffff == b & 0xffff
}
puts c
a = A
b = B
c = 0
5_000_000.times {
a = a * AM % MOD
a = a * AM % MOD until a % 4 == 0
b = b * BM % MOD
b = b * BM % MOD until b % 8 == 0
c += 1 if a & 0xffff == b & 0xffff
}
puts c