Skip to content

Commit 51378b1

Browse files
committed
Day 15
* $ time ruby 15a.rb MRI: 16s GraalVM --native: 7s With Integer#times written in Ruby to enable OSR: 2s
1 parent b9b5bd6 commit 51378b1

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

2017/15a.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
starts = [591, 393]
2+
factors = [16807, 48271]
3+
4+
Generator = Struct.new(:start, :factor) do
5+
def initialize(*)
6+
super
7+
@n = start
8+
end
9+
10+
def next
11+
@n = (@n * factor) % 2147483647
12+
end
13+
end
14+
15+
generators = starts.zip(factors).map { |start, factor|
16+
Generator.new(start, factor)
17+
}
18+
19+
p 40_000_000.times.count {
20+
a, b = generators.map { |gen| gen.next % 2**16 }
21+
a == b
22+
}

2017/15b.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
starts = [591, 393]
2+
factors = [16807, 48271]
3+
multiples = [4, 8]
4+
5+
Generator = Struct.new(:start, :factor, :multiple) do
6+
def initialize(*)
7+
super
8+
@n = start
9+
end
10+
11+
def next
12+
begin
13+
@n = (@n * factor) % 2147483647
14+
end until @n % multiple == 0
15+
@n
16+
end
17+
end
18+
19+
generators = starts.zip(factors, multiples).map { |start, factor, multiple|
20+
Generator.new(start, factor, multiple)
21+
}
22+
23+
p 5_000_000.times.count {
24+
a, b = generators.map { |gen| gen.next % 2**16 }
25+
a == b
26+
}

0 commit comments

Comments
 (0)