Skip to content

Commit

Permalink
Day 15
Browse files Browse the repository at this point in the history
* $ time ruby 15a.rb
  MRI: 16s
  GraalVM --native: 7s
  With Integer#times written in Ruby to enable OSR: 2s
  • Loading branch information
eregon committed Dec 16, 2017
1 parent b9b5bd6 commit 51378b1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 2017/15a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
starts = [591, 393]
factors = [16807, 48271]

Generator = Struct.new(:start, :factor) do
def initialize(*)
super
@n = start
end

def next
@n = (@n * factor) % 2147483647
end
end

generators = starts.zip(factors).map { |start, factor|
Generator.new(start, factor)
}

p 40_000_000.times.count {
a, b = generators.map { |gen| gen.next % 2**16 }
a == b
}
26 changes: 26 additions & 0 deletions 2017/15b.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
starts = [591, 393]
factors = [16807, 48271]
multiples = [4, 8]

Generator = Struct.new(:start, :factor, :multiple) do
def initialize(*)
super
@n = start
end

def next
begin
@n = (@n * factor) % 2147483647
end until @n % multiple == 0
@n
end
end

generators = starts.zip(factors, multiples).map { |start, factor, multiple|
Generator.new(start, factor, multiple)
}

p 5_000_000.times.count {
a, b = generators.map { |gen| gen.next % 2**16 }
a == b
}

0 comments on commit 51378b1

Please sign in to comment.