Advertisement
mscha

First attempt AoC 2017 day 15

Dec 15th, 2017
3,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.82 KB | None | 0 0
  1. #!/usr/bin/env perl6
  2. use v6.c;
  3.  
  4. # Advent of Code 2017, day 15: http://adventofcode.com/2017/day/15
  5.  
  6. # Use with an input file containing two numbers, e.g.
  7. # 65 8921
  8.  
  9. sub generator(int $init, int $times, int $modulo)
  10. {
  11.     my int $value = $init;
  12.     lazy gather loop {
  13.         $value = ($value × $times) % $modulo;
  14.         take $value;
  15.     }
  16. }
  17.  
  18. multi sub MAIN(IO() $inputfile where *.f)
  19. {
  20.     my ($init-A, $init-B) = $inputfile.slurp.comb(/\d+/)».Int;
  21.     my @gen-A = generator($init-A, 16807, 2147483647);
  22.     my @gen-B = generator($init-B, 48271, 2147483647);
  23.  
  24.     # Part 1
  25.     my $count = 0;
  26.     for ^40_000_0 -> $i {
  27.         $count++ if @gen-A[$i] +& 65535 == @gen-B[$i] +& 65535;
  28.     }
  29.     say "Judge's final count: $count";
  30. }
  31.  
  32. multi sub MAIN()
  33. {
  34.     MAIN($*PROGRAM.parent.child('aoc15.input'));
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement