diff options
Diffstat (limited to 'day15')
-rw-r--r-- | day15/input | 2 | ||||
-rwxr-xr-x | day15/part1 | 31 | ||||
-rwxr-xr-x | day15/part2 | 36 |
3 files changed, 69 insertions, 0 deletions
diff --git a/day15/input b/day15/input new file mode 100644 index 0000000..46491bf --- /dev/null +++ b/day15/input @@ -0,0 +1,2 @@ +Generator A starts with 618 +Generator B starts with 814 diff --git a/day15/part1 b/day15/part1 new file mode 100755 index 0000000..067ee05 --- /dev/null +++ b/day15/part1 @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby + +class Generator + def initialize(iv, factor) + @value = iv + @factor = factor + end + + def next + n = (@value * @factor) % 2147483647 + @value = n + return n + end +end + +input = $stdin.readlines.map(&:chomp) + +ga = Generator.new(input[0].split[4].to_i, 16807) +gb = Generator.new(input[1].split[4].to_i, 48271) + +matches = 0 + +Mask = 2**16 + +40_000_000.times do + a = ga.next % Mask + b = gb.next % Mask + matches += 1 if a == b +end + +p matches 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 |