mscha icon

First attempt AoC 2017 day 15

mscha | PRO | 12/15/17 10:10:48 AM UTC | 0 ⭐ | 11985 👁️ | Never ⏰ | []
Perl 6 |

0 B

|

None

|

0 👍

/

0 👎

#!/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

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎