diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-12-14 00:12:25 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-12-14 00:12:25 -0500 |
| commit | 035bdaeeef58a57a973a144bfe396f0c2c683fe2 (patch) | |
| tree | c2f181036da0b13eec9c6751a34b027115c557a7 /challenge-143/dave-jacoby | |
| parent | ecafe0e840b90c1462ec4ac924f3255dd3beb2c0 (diff) | |
| download | perlweeklychallenge-club-035bdaeeef58a57a973a144bfe396f0c2c683fe2.tar.gz perlweeklychallenge-club-035bdaeeef58a57a973a144bfe396f0c2c683fe2.tar.bz2 perlweeklychallenge-club-035bdaeeef58a57a973a144bfe396f0c2c683fe2.zip | |
Done!
Diffstat (limited to 'challenge-143/dave-jacoby')
| -rw-r--r-- | challenge-143/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-143/dave-jacoby/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-143/dave-jacoby/perl/ch-2.pl | 41 |
3 files changed, 104 insertions, 0 deletions
diff --git a/challenge-143/dave-jacoby/blog.txt b/challenge-143/dave-jacoby/blog.txt new file mode 100644 index 0000000000..c3e15f2677 --- /dev/null +++ b/challenge-143/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/12/14/ninja-numbers-hiding-in-trees-the-weekly-challenge-143.html diff --git a/challenge-143/dave-jacoby/perl/ch-1.pl b/challenge-143/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..0caded391c --- /dev/null +++ b/challenge-143/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +my @examples; +push @examples, '( 5 * 10 ) - ( 12 * 3 )'; +push @examples, '10 + 20 - 5'; +push @examples, '(10 + 20 - 5) * 2'; +push @examples, '( ( 10 * 20 ) - 5) * 2'; + +for my $i (@examples) { + my $o = calculator($i); + my $o2 = bc($i); + say <<"END"; + Input: \$i = $i + Output: $o + BC: $o2 +END +} + +sub calculator( $s) { + + # parens + while ( $s =~ /\([\s\d\+\-\*]+\)/mix ) { + $s =~ s/\(([\s\d\+\-\*]+\))/calculator( unbracket( $1 ))/e; + } + + # multiplication + + while ( $s =~ / \d+ \s* \* \s* \d+ /mx ) { + $s =~ s/( (\d+) \s* \* \s* (\d+) )/ $2 * $3 /emx; + } + + # addition + + while ( $s =~ / \d+ \s* \+ \s* \d+ /mx ) { + $s =~ s/( (\d+) \s* \+ \s* (\d+) )/ $2 + $3 /emx; + } + + # subtraction + while ( $s =~ / \d+ \s* \- \s* \d+ /mx ) { + $s =~ s/( (\d+) \s* \- \s* (\d+) )/ $2 - $3 /emx; + } + return $s; +} + +sub unbracket( $s ) { + $s =~ s/^\(//; + $s =~ s/\)$//; + return $s; +} + +# This is the easy way, using pre-existing code +sub bc( $s) { + my $cmd = qq{ echo '$s' | bc }; + my $x = qx{$cmd}; + chomp $x; + return $x; +} diff --git a/challenge-143/dave-jacoby/perl/ch-2.pl b/challenge-143/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..c712e2014b --- /dev/null +++ b/challenge-143/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +my @examples = qw( 6 12 24 36 ); + +for my $i (@examples) { + my $o = stealthy_numbers($i); + say <<"END"; + Input: \$n = $i + Output: $o +END +} + +sub stealthy_numbers ( $n ) { + my @factors = get_factor_pairs($n); + for my $i ( 0 .. -1 + scalar @factors ) { + my ( $ix, $iy ) = $factors[$i]->@*; + for my $j ( $i + 1 .. -1 + scalar @factors ) { + my ( $jx, $jy ) = $factors[$j]->@*; + my $addi = $ix + $iy; + my $addj = $jx + $jy; + return 1 if abs( $addi - $addj ) == 1; + } + } + return 0; +} + +sub get_factor_pairs( $n ) { + my %hash; + for my $x ( map { int $_ } 1 .. $n ) { + next unless $n % $x == 0; + my $y = $n / $x; + my $xy = join ',', sort { $a <=> $b } $x, $y; + $hash{$xy} = 1; + } + return map { [ split /,/, $_ ] } sort keys %hash; +} |
