#!/usr/bin/env perl6
use v6.c;
# Advent of Code 2017, day 15: http://adventofcode.com/2017/day/15
# Use with an input file containing two numbers, e.g.
# 65 8921
sub generator(int $init, int $times, int $modulo)
{
my int $value = $init;
lazy gather loop {
$value = ($value × $times) % $modulo;
take $value;
}
}
multi sub MAIN(IO() $inputfile where *.f)
{
my ($init-A, $init-B) = $inputfile.slurp.comb(/\d+/)».Int;
my @gen-A = generator($init-A, 16807, 2147483647);
my @gen-B = generator($init-B, 48271, 2147483647);
# Part 1
my $count = 0;
for ^40_000_0 -> $i {
$count++ if @gen-A[$i] +& 65535 == @gen-B[$i] +& 65535;
}
say "Judge's final count: $count";
}
multi sub MAIN()
{
MAIN($*PROGRAM.parent.child('aoc15.input'));
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎