From 999db74bb78ccded11230933ff5d247c51040a29 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Fri, 15 Dec 2017 23:14:10 +0000 Subject: Day15 --- day15/part2 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 day15/part2 (limited to 'day15/part2') diff --git a/day15/part2 b/day15/part2 new file mode 100755 index 0000000..7253a39 --- /dev/null +++ b/day15/part2 @@ -0,0 +1,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 -- cgit v1.2.3