diff options
| author | Abigail <abigail@abigail.be> | 2021-06-30 00:44:09 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-06-30 00:44:09 +0200 |
| commit | 20c14072c2b9a491fd414d275b6c64c6fec8e62f (patch) | |
| tree | 8de976c75da6f68a0c9ee91809dcc3a340358fdc /challenge-119/dave-jacoby/perl/ch-2.pl | |
| parent | a6180cd9e375d5f36d77f7a2aac2650d5c642058 (diff) | |
| parent | 551f8dc93afa46a598644f62e74e67cdae6f0c28 (diff) | |
| download | perlweeklychallenge-club-20c14072c2b9a491fd414d275b6c64c6fec8e62f.tar.gz perlweeklychallenge-club-20c14072c2b9a491fd414d275b6c64c6fec8e62f.tar.bz2 perlweeklychallenge-club-20c14072c2b9a491fd414d275b6c64c6fec8e62f.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-119/dave-jacoby/perl/ch-2.pl')
| -rw-r--r-- | challenge-119/dave-jacoby/perl/ch-2.pl | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-119/dave-jacoby/perl/ch-2.pl b/challenge-119/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..702ef4b13a --- /dev/null +++ b/challenge-119/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ postderef say signatures state }; +no warnings qw{ experimental }; + +use Memoize; +memoize('first_pass'); + +my @list = map { int } @ARGV; +@list = ( 2, 5, 10, 60, 200 ) unless scalar @list; + +for my $n (@list) { + say join "\t", '', $n, solve_sequence($n); +} + +# here we get an array such that index $n is in the array +# using increasingly aggressive methods, then +sub solve_sequence( $n ) { + my $j = $n * 2; + my @sequence = get_sequence( 1 .. $j ); + while ( !$sequence[$n] ) { + $j = $j * 2; + @sequence = get_sequence( 1 .. $j ); + } + return $sequence[$n]; +} + +# the next things we want to do are to remove the blocked numbers +# which contain either 0 or 11, and then add another entry to the +# start of the array so that 1 aligns with 1. +sub get_sequence( @arr ) { + my @seq = + grep { !/11/ } + grep { !/0/ } + map { first_pass($_) } @arr; + unshift @seq, ''; + return @seq; +} + +# the numbers will contain only the digits 1, 2 and 3, so to limit +# the amount of numbers we have to come up with, I first make everything +# base 4. This function is memoizable and so I memoized it. +sub first_pass ( $n ) { + return $n if $n == 0; + my @output; + while ($n) { + my $i = $n % 4; + $n = int $n / 4; + push @output, $i; + } + return join '', reverse @output; +} |
