diff --git a/15/1.pl b/15/1.pl new file mode 100644 index 0000000..a0c979f --- /dev/null +++ b/15/1.pl @@ -0,0 +1,18 @@ +use 5.20.0; + +use List::AllUtils qw/ pairgrep /; + +my @v = ( 679,771 ); + +my $mask = 2**16 - 1; + +my $match; +for ( 1..40_000_000 ) { + @v = ( 16807 * $v[0], 48271 * $v[1] ); + $_ %= 2147483647 for @v; + + $match += ($v[0] & $mask) == ($v[1] & $mask); + +} + +say $match; diff --git a/15/2.pl b/15/2.pl new file mode 100644 index 0000000..72cbdfb --- /dev/null +++ b/15/2.pl @@ -0,0 +1,27 @@ +use 5.20.0; + +use List::Lazy qw/ lazy_list lazy_range /; + +my $mask = 2**16 - 1; + +my $list_a = ( lazy_list { + $_ *= 16_807; + $_ %= 2147483647; + $_; +} 679 )->grep( sub { not $_ % 4 } )->map(sub{ $_ & $mask }); + +my $list_b = ( lazy_list { + $_ *= 48_271; + $_ %= 2147483647; + $_; +} 771 )->grep( sub { not $_ % 8 } )->map(sub{ $_ & $mask }); + + +use DDP; +my $i = 0; +say $list_a + ->until(sub{ ++$i >= 5_000_000}) + ->spy(sub { say $i unless $i % 100_000 }) + ->grep(sub { $_ == $list_b->next } ) + ->reduce(sub { $a + 1},0); + diff --git a/15/input.txt b/15/input.txt new file mode 100644 index 0000000..10e3bb0 --- /dev/null +++ b/15/input.txt @@ -0,0 +1,2 @@ +Generator A starts with 679 +Generator B starts with 771