Skip to content

Solutions to challenge #69. #1939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions challenge-069/roger-bell-west/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! /usr/bin/perl

use strict;
use warnings;

use Test::More tests => 1;

is_deeply(sg(50,100),
[69,88,96],
'example 1',
);

sub sg {
my @range=@_;
my @out;
my %charges=(
6 => 9,
9 => 6,
8 => 8,
0 => 0,
);
my $cm=join('',keys %charges);
OUTER:
foreach my $n ($range[0]..$range[1]) {
if ($n !~ /^[$cm]*$/) {
next;
}
if ($n =~ /0$/) {
next;
}
my $nl=length($n)-1;
my %locs;
foreach my $pos (0..$nl) {
$locs{$nl-$pos}=$charges{substr($n,$pos,1)};
}
foreach my $pos (keys %locs) {
if (substr($n,$pos,1) ne $locs{$pos}) {
next OUTER;
}
}
push @out,$n;
}
return \@out;
}
40 changes: 40 additions & 0 deletions challenge-069/roger-bell-west/perl/ch-2a.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#! /usr/bin/perl

use strict;
use warnings;

my $s='';
my $n=0;
while (1) {
print "S$n = \"$s\"\n";
my @i;
my $l=-1;
do {
$l=index($s,'1',$l+1);
if ($l>-1) {
push @i,$l+1;
}
} while ($l!=-1);
print join(',',@i),"\n";
$s=plusone($s);
$n++;
sleep 1;
}

sub plusone {
my $in=shift;
return join('',$in,'0',switch(rev($in)));
}

sub switch {
my $str=shift;
$str =~ s/1/x/g;
$str =~ s/0/1/g;
$str =~ s/x/0/g;
return $str;
}

sub rev {
my @in=split '',shift;
return join('',reverse @in);
}
47 changes: 47 additions & 0 deletions challenge-069/roger-bell-west/perl/ch-2b.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#! /usr/bin/perl

use strict;
use warnings;

use List::Util qw(min);

my $s=1000;
my $l=2**$s-1;

my $pos=1;
my $seq=4;
while (1) {
my $p1=$seq-1;
my $n0=min($p1-$pos,$l-$pos);
print '0' x $n0;
$pos+=$n0;
if ($p1<=$l) {
print '1';
$pos++;
}
if ($pos>$l) {
last;
}
$seq=nextseq($seq);
}
print "\n";

sub nextseq {
my $prev=shift;
my $c=$prev;
while (1) {
$c++;
my $t=$c-1;
while (1) {
my $r=int($t/2);
if ($t % 2 != 0) {
last;
}
$t=$r;
}
if ($t % 4 == 3) {
last;
}
}
return $c;
}
45 changes: 45 additions & 0 deletions challenge-069/roger-bell-west/raku/ch-1.p6
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#! /usr/bin/perl6

use Test;

plan 1;

is-deeply(sg(50,100),
[69,88,96],
'example 1',
);

sub sg ($a,$b) {
my @out;
my %charges=(
6 => 9,
9 => 6,
8 => 8,
0 => 0,
);
my $cm='^<[' ~ %charges.keys.join('') ~ ']>*$';
for $a..$b -> $n {
if ($n !~~ /<$cm>/) {
next;
}
if ($n ~~ /0$/) {
next;
}
my $nl=chars($n)-1;
my %locs;
for 0..$nl -> $pos {
%locs{$nl-$pos}=%charges{substr($n,$pos,1)};
}
my $nn=$n;
for keys %locs -> $pos {
if (substr($n,$pos,1) ne %locs{$pos}) {
$nn=-1;
last;
}
}
if ($nn>=0) {
push @out,$nn;
}
}
return @out;
}
41 changes: 41 additions & 0 deletions challenge-069/roger-bell-west/raku/ch-2b.p6
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /usr/bin/perl6

my $s=1000;
my $l=2**$s-1;

my $pos=1;
my $seq=4;
while (1) {
my $p1=$seq-1;
my $n0=min($p1-$pos,$l-$pos);
print '0' x $n0;
$pos+=$n0;
if ($p1 <= $l) {
print '1';
$pos++;
}
if ($pos > $l) {
last;
}
$seq=nextseq($seq);
}
print "\n";

sub nextseq ($prev) {
my $c=$prev;
while (1) {
$c++;
my $t=$c-1;
while (1) {
my $r=truncate($t/2);
if ($t % 2 != 0) {
last;
}
$t=$r;
}
if ($t % 4 == 3) {
last;
}
}
return $c;
}
5 changes: 5 additions & 0 deletions challenge-069/roger-bell-west/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
For problem #2:

ch-2a is the utility code that enabled me to find the solution.

ch-2b is the code that outputs the desired answer (yes, even for S=1000).