aboutsummaryrefslogtreecommitdiff
path: root/day15/part2
blob: 7253a39f9047a0177f2830a6ba7bb7ce26139944 (plain)
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
#!/usr/bin/env ruby

class Generator
  def initialize(iv, factor, divisor)
    @value = iv
    @factor = factor
    @divisor = divisor
  end

  def next
    n = @value
    loop do
      n = (n * @factor) % 2147483647
      break if n % @divisor == 0
    end
    @value = n
    return n
  end
end

input = $stdin.readlines.map(&:chomp)

ga = Generator.new(input[0].split[4].to_i, 16807, 4)
gb = Generator.new(input[1].split[4].to_i, 48271, 8)

matches = 0

Mask = 2**16

5_000_000.times do
  a = ga.next % Mask
  b = gb.next % Mask
  matches += 1 if a == b
end

p matches