aboutsummaryrefslogtreecommitdiff
path: root/challenge-119
diff options
context:
space:
mode:
authorJared Martin <jaredor+github@gmail.com>2021-07-04 17:24:49 -0500
committerJared Martin <jaredor+github@gmail.com>2021-07-04 17:24:49 -0500
commitadb2b794cacc1233eb710dddfe540591c37658ed (patch)
tree593a4b9cc630eb4bcb7af8f3ee4250055b15c160 /challenge-119
parentf2ac6d61dd95c4e1269e1ec1dbaabb505906d27e (diff)
parentf9991b877b6ff808ea4c60dcc7833581c2c2494f (diff)
downloadperlweeklychallenge-club-adb2b794cacc1233eb710dddfe540591c37658ed.tar.gz
perlweeklychallenge-club-adb2b794cacc1233eb710dddfe540591c37658ed.tar.bz2
perlweeklychallenge-club-adb2b794cacc1233eb710dddfe540591c37658ed.zip
I have no freaking clue as to what is going on. I just want to push some changes to github. !@#$!@#!$$#~
Diffstat (limited to 'challenge-119')
-rw-r--r--challenge-119/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-119/jaldhar-h-vyas/perl/ch-1.pl13
-rwxr-xr-xchallenge-119/jaldhar-h-vyas/perl/ch-2.pl16
-rwxr-xr-xchallenge-119/jaldhar-h-vyas/raku/ch-1.raku12
-rwxr-xr-xchallenge-119/jaldhar-h-vyas/raku/ch-2.sh1
-rw-r--r--challenge-119/james-smith/README.md40
-rw-r--r--challenge-119/james-smith/cesil/cesil.pl38
7 files changed, 75 insertions, 46 deletions
diff --git a/challenge-119/jaldhar-h-vyas/blog.txt b/challenge-119/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..9b34185ba3
--- /dev/null
+++ b/challenge-119/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2021/07/perl_weekly_challenge_week_119.html
diff --git a/challenge-119/jaldhar-h-vyas/perl/ch-1.pl b/challenge-119/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..e8c03576bd
--- /dev/null
+++ b/challenge-119/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+my $N = shift // die "Must specify an integer.\n";
+
+my $n = sprintf "%b", $N;
+if ((length $n) % 8 != 0) {
+ $n = 0 x (8 - (length $n) % 8) . $n;
+}
+
+say oct '0b' . join q{}, reverse (unpack '(A4)*', $n);
+
diff --git a/challenge-119/jaldhar-h-vyas/perl/ch-2.pl b/challenge-119/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..e594aee380
--- /dev/null
+++ b/challenge-119/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+my $N = shift // die "Must specify an integer.\n";
+my $n = 0;
+my $nth = 0;
+
+while (++$n) {
+ if ( $n =~ /^ [1-3]+ $/x && $n !~ /^ 11/x ) {
+ if (++$nth == $N) {
+ say $n;
+ last;
+ }
+ }
+} \ No newline at end of file
diff --git a/challenge-119/jaldhar-h-vyas/raku/ch-1.raku b/challenge-119/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..9e05b654fa
--- /dev/null
+++ b/challenge-119/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,12 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ Int $N #= an integer
+) {
+ my $n = $N.base(2);
+ if $n.chars !%% 8 {
+ $n = 0 x (8 - $n.chars % 8) ~ $n;
+ }
+
+ $n.comb(4).reverse.join.parse-base(2).say;
+} \ No newline at end of file
diff --git a/challenge-119/jaldhar-h-vyas/raku/ch-2.sh b/challenge-119/jaldhar-h-vyas/raku/ch-2.sh
new file mode 100755
index 0000000000..5cfdfb39b8
--- /dev/null
+++ b/challenge-119/jaldhar-h-vyas/raku/ch-2.sh
@@ -0,0 +1 @@
+raku -e '(1 .. *).grep({ /^ <[1 .. 3]>+ $/ && !/^ 11/ })[@*ARGS[0] - 1].say' $@ \ No newline at end of file
diff --git a/challenge-119/james-smith/README.md b/challenge-119/james-smith/README.md
index 84f389fefc..11f1a24015 100644
--- a/challenge-119/james-smith/README.md
+++ b/challenge-119/james-smith/README.md
@@ -138,40 +138,35 @@ interpreter... This uses a dispatch table to execute the commands
- a list of "anonymous" subroutines stored in a hash.
```perl
-
use strict;
use warnings;
-$| = 1; # Set auto-flush...
## Initialize state
-my($MAX_LOOPS, $ptr, $reg, @in, %mem, @code, %ptrs) = (1e6,0,0);
+my($MAX,$ptr,$reg,@in,%mem,@code,%ptrs)=(1e6,0,0);
## Define error messages
my %messages = (
- 'i' => 'No further input',
- 'd' => 'Division by zero ',
- 'm' => 'Unitialized memory at ',
- 'l' => 'Unknown pointer ',
-);
+'i','No further input','d','Division by zero ',
+'l','Unknown pointer ','m','Unitialized memory at ');
## Support functions
-sub _err { die sprintf "\n** %s%s [cmd %s - line %d]\n",
- $messages{$_[0]}, $code[$ptr][1], $code[$ptr][0], 1+$ptr; }
-sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_err 'l'; }
-sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_err 'm'; }
+sub _e { die sprintf "\n** %s%s [%s @ %d]\n",
+ $messages{$_[0]},@{$code[$ptr]}[1,0],1+$ptr}
+sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_e 'l'}
+sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_e 'm'}
## Command dispatch table
my %commands = (
'LINE' ,sub{print "\n"},
'OUT' ,sub{print $reg},
'PRINT' ,sub{print s/^"//r=~s/"$//r},
-'IN' ,sub{@in?($reg=shift@in):_err 'i'},
+'IN' ,sub{@in?($reg=shift@in):_e 'i'},
'STORE' ,sub{$mem{$_}=$reg},
'LOAD' ,sub{$reg=_v},
'ADD' ,sub{$reg+=_v},
'SUBTRACT',sub{$reg-=_v},
'MULTIPLY',sub{$reg*=_v},
-'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_err 'd'},
+'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_e 'd'},
'JINEG' ,sub{_j if $reg<0},
'JIZERO' ,sub{_j if !$reg},
'JUMP' ,sub{_j},
@@ -180,20 +175,17 @@ my %commands = (
## Parser loop
while(<>) {
- ((@in = map {/^\s+-?\d+\s*$/?0+$_:()} <> ),last) if m{^ {8}%};
- ($ptrs{$1},$_)=(scalar @code,$2) if m{^(\S{1,7})\s+(.*)};
- my($cmd,$data) = split m{\s+}, s{^\s+}{}r=~s{\s+$}{}r, 2;
- die "\n** Unknown command [cmd $cmd - line ",1+@code,"]\n"
+ ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/;
+ ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/;
+ my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2;
+ die "\n## Unknown command [$cmd @ ",1+@code,"]\n"
unless exists $commands{$cmd};
- push @code, [$cmd,$data||''];
+ push @code, [$cmd,$data//''];
}
-
## Execution loop
($commands{$code[$ptr][0]}($_=$code[$ptr][1]),$ptr++)
- while --$MAX_LOOPS && $ptr<@code;
-
-## Error if did not exit with HALT..
-die "\n** Exited without HALT\n" if $ptr >= @code;
+ while--$MAX&& $ptr<@code;
+die "\n** Exited without HALT\n";
```
# Task 2 - Sequence without 1-on-1
diff --git a/challenge-119/james-smith/cesil/cesil.pl b/challenge-119/james-smith/cesil/cesil.pl
index 25e79e8982..64b0355de7 100644
--- a/challenge-119/james-smith/cesil/cesil.pl
+++ b/challenge-119/james-smith/cesil/cesil.pl
@@ -1,37 +1,32 @@
use strict;
use warnings;
-# Set auto-flush...
-$| = 1;
## Initialize state
-my($MAX_LOOPS, $ptr, $reg, @in, %mem, @code, %ptrs) = (1e6,0,0);
+my($MAX,$ptr,$reg,@in,%mem,@code,%ptrs)=(1e6,0,0);
## Define error messages
my %messages = (
- 'i' => 'No further input',
- 'd' => 'Division by zero ',
- 'm' => 'Unitialized memory at ',
- 'l' => 'Unknown pointer ',
-);
+'i','No further input','d','Division by zero ',
+'l','Unknown pointer ','m','Unitialized memory at ');
## Support functions
-sub _err { die sprintf "\n** %s%s [cmd %s - line %d]\n",
- $messages{$_[0]}, $code[$ptr][1], $code[$ptr][0], 1+$ptr; }
-sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_err 'l'; }
-sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_err 'm'; }
+sub _e { die sprintf "\n** %s%s [%s @ %d]\n",
+ $messages{$_[0]},@{$code[$ptr]}[1,0],1+$ptr}
+sub _j { exists$ptrs{$_}?($ptr=$ptrs{$_}-1):_e 'l'}
+sub _v { /^-?\d+$/?$_:exists$mem{$_}?$mem{$_}:_e 'm'}
## Command dispatch table
my %commands = (
'LINE' ,sub{print "\n"},
'OUT' ,sub{print $reg},
'PRINT' ,sub{print s/^"//r=~s/"$//r},
-'IN' ,sub{@in?($reg=shift@in):_err 'i'},
+'IN' ,sub{@in?($reg=shift@in):_e 'i'},
'STORE' ,sub{$mem{$_}=$reg},
'LOAD' ,sub{$reg=_v},
'ADD' ,sub{$reg+=_v},
'SUBTRACT',sub{$reg-=_v},
'MULTIPLY',sub{$reg*=_v},
-'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_err 'd'},
+'DIVIDE' ,sub{$_=_v;$reg=$_?int($reg/$_):_e 'd'},
'JINEG' ,sub{_j if $reg<0},
'JIZERO' ,sub{_j if !$reg},
'JUMP' ,sub{_j},
@@ -40,16 +35,15 @@ my %commands = (
## Parser loop
while(<>) {
- ((@in = map {/^\s+-?\d+\s*$/?0+$_:()} <> ),last) if m{^ {8}%};
- ($ptrs{$1},$_)=(scalar @code,$2) if m{^(\S{1,7})\s+(.*)};
- my($cmd,$data) = split m{\s+}, s{^\s+}{}r=~s{\s+$}{}r, 2;
- die "\n** Unknown command [cmd $cmd - line ",1+@code,"]\n"
+ ((@in=map{/^\s+-?\d+\s*$/?0+$_:()}<>),last)if/^ {8}%/;
+ ($ptrs{$1},$_)=(0+@code,$2) if m/^(\S{1,7})\s+(.*)/;
+ my($cmd,$data) = split/\s+/,s/^\s+//r=~s/\s+$//r, 2;
+ die "\n## Unknown command [$cmd @ ",1+@code,"]\n"
unless exists $commands{$cmd};
- push @code, [$cmd,$data||''];
+ push @code, [$cmd,$data//''];
}
-
## Execution loop
($commands{$code[$ptr][0]}($_=$code[$ptr][1]),$ptr++)
- while --$MAX_LOOPS && $ptr<@code;
-die "\n** Exited without HALT\n" if $ptr >= @code;
+ while--$MAX&& $ptr<@code;
+die "\n** Exited without HALT\n";