diff options
| author | andrezgz <andrezgz@gmail.com> | 2020-01-23 19:16:36 -0300 |
|---|---|---|
| committer | andrezgz <andrezgz@gmail.com> | 2020-01-23 19:16:36 -0300 |
| commit | d42b68c32d420eedcb4940793747c901fec65c54 (patch) | |
| tree | 06a0c26d2c579f19c5ec8b93912ac991f0c8f9f6 /challenge-044 | |
| parent | 87a6af9725acf75343b1d590e224d9cfbd5f0cd8 (diff) | |
| download | perlweeklychallenge-club-d42b68c32d420eedcb4940793747c901fec65c54.tar.gz perlweeklychallenge-club-d42b68c32d420eedcb4940793747c901fec65c54.tar.bz2 perlweeklychallenge-club-d42b68c32d420eedcb4940793747c901fec65c54.zip | |
challenge-044 andrezgz solution
Diffstat (limited to 'challenge-044')
| -rw-r--r-- | challenge-044/andrezgz/perl/ch-1.pl | 60 | ||||
| -rw-r--r-- | challenge-044/andrezgz/perl/ch-2.pl | 56 |
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-044/andrezgz/perl/ch-1.pl b/challenge-044/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..a4d03596cc --- /dev/null +++ b/challenge-044/andrezgz/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-044/ +# Task #1 +# Only 100, please. +# You are given a string "123456789". +# Write a script that would insert "+" or "-" in between digits +# so that when you evaluate, the result should be 100. + +use strict; +use warnings; + +my @digits = split //, '123456789'; +my $initial = shift @digits; + +my %values = ( 0 => '', 1 => '+', 2 => '-' ); + +# A number in base 3 is used to get a particular combination +# for each of the 8 positions between the 9 digits +# The combination is left padded with zeros to get exactly 8 digits for lower numbers +# +# The 8 digits from a combination define an equation +# translating each one to its value: +, - or '' (empty) +# and concatenating it with the corresponding digit from the given number + +foreach my $n (0 .. (3**8)-1) { + my $equation = $initial; + my @combination = split //, sprintf "%08d", to_base3($n); + $equation .= $values{ $combination[$_] } . $digits[$_] for (0 .. @digits - 1); + print "$equation\n" if (eval $equation == 100); +} + + +sub to_base3 { + my ($n) = @_; + return 0 if ($n == 0); + + my $result; + while ($n > 0) { + $result .= $n % 3; + $n = int($n / 3); + } + # force scalar context to assure returning a reversed string + return scalar reverse $result; +} + +__END__ + +./ch-1.pl +123+45-67+8-9 +123+4-5+67-89 +123-45-67+89 +123-4-5-6-7+8-9 +12+3+4+5-6-7+89 +12+3-4+5+67+8+9 +12-3-4+5-6+7+89 +1+23-4+56+7+8+9 +1+23-4+5+6+78-9 +1+2+34-5+67-8+9 +1+2+3-4+5+6+78+9 diff --git a/challenge-044/andrezgz/perl/ch-2.pl b/challenge-044/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..2a8e999576 --- /dev/null +++ b/challenge-044/andrezgz/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-044/ +# Task #2 +# Make it $200 +# You have only $1 left at the start of the week. +# You have been given an opportunity to make it $200. +# The rule is simple with every move you can either double what you have or add another $1. +# Write a script to help you get $200 with the smallest number of moves. + +use strict; +use warnings; + +# an upper bound of moves to check +use constant MOVES_LIMIT => 10; + +my @solution; + +# A binary number is used to get a particular combination +# for each of the moves to check +# +# The combination defines a serie of operations (add 1 or multiply by 2) +# Each one of them updates the value for the next operation. + +foreach my $n ( 0 .. (2 ** MOVES_LIMIT)-1 ) { + my @ops = map { $_ ? '+ 1' : '* 2'} split //, sprintf("%b", $n); + + my $value = 1; + $value = eval($value . $_) for (@ops); + + if ($value == 200) { + @solution = @ops; + last; + } +} + +# beautified solution printing +my $value = 1; +print $value . $/; +foreach (@solution) { + $value = eval($value . $_); + print $_.' = '. $value . $/; +} + +__END__ +./ch-2.pl +1 ++ 1 = 2 ++ 1 = 3 +* 2 = 6 +* 2 = 12 +* 2 = 24 ++ 1 = 25 +* 2 = 50 +* 2 = 100 +* 2 = 200 |
