Skip to content

Commit dcf0f13

Browse files
committed
rjt's Week 048 solutions and blogs
1 parent 49ac62f commit dcf0f13

File tree

7 files changed

+100
-6
lines changed

7 files changed

+100
-6
lines changed

challenge-048/ryan-thompson/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Ryan Thompson
22

3-
## Week 047 Solutions
3+
## Week 048 Solutions
44

5-
### Task 1 › Roman Calculator
5+
### Task 1 › Survivor
66

77
* [Perl](perl/ch-1.pl)
8-
* **Raku:** No Raku solution this week, sorry.
8+
* [Raku](raku/ch-1.p6)
99

10-
### Task 2 › Gapful Numbers
10+
### Task 2 › Palindrome Dates
1111

1212
* [Perl](perl/ch-2.pl)
1313
* [Raku](raku/ch-2.p6)
1414

1515
## Blogs
1616

17-
* [Task 1 › Roman Calculator](http://www.ry.ca/2020/02/roman-calculator/)
18-
* [Task 2 › Gapful Numbers](http://www.ry.ca/2020/02/gapful-numbers/)
17+
* [Task 1 › Survivor](http://www.ry.ca/2020/02/survivor-josepheus-problem/)
18+
* [Task 2 › Palindrome Dates](http://www.ry.ca/2020/02/palindrome-dates/)

challenge-048/ryan-thompson/blog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://www.ry.ca/2020/02/survivor-josepheus-problem/

challenge-048/ryan-thompson/blog1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://www.ry.ca/2020/02/palindrome-dates/
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env perl
2+
#
3+
# ch-1.pl - Survivor
4+
#
5+
# Ryan Thompson <[email protected]>
6+
7+
use 5.010;
8+
use warnings;
9+
use strict;
10+
11+
say survivor(shift) and exit if @ARGV;
12+
13+
# Linked list solution
14+
sub survivor {
15+
my $N = shift;
16+
my @ll = (undef, 2..$N, 1); # Circular linked list
17+
my $cur = 1;
18+
$ll[$cur] = $ll[$ll[$cur]], $cur = $ll[$cur] until $ll[$cur] == $cur;
19+
20+
$cur;
21+
}
22+
23+
sub analytic {
24+
my $N = shift;
25+
2 * ($N - 2**int( log($N) / log(2) )) + 1;
26+
}
27+
28+
#
29+
# Benchmarking and Tests
30+
#
31+
32+
use Benchmark qw/cmpthese/;
33+
use Test::More;
34+
35+
cmpthese(-5, {
36+
linked => sub { survivor($_) for 1..100 },
37+
analytic => sub { analytic($_) for 1..100 },
38+
});
39+
40+
is survivor(100), 73;
41+
is survivor(50), 37;
42+
is survivor($_), analytic($_) for 1..100;
43+
done_testing;
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env perl
2+
#
3+
# ch-2.pl - Palindrome Dates (mm/dd/yyyy)
4+
#
5+
# Ryan Thompson <[email protected]>
6+
7+
use warnings;
8+
use strict;
9+
10+
# All of the 2-digit years must also be months
11+
my @yy = sort map { chop . ($_||0) } 1..12;
12+
13+
# Ensure printing in the correct order, with minimal looping
14+
for my $dd (qw<02 12 22>) {
15+
for my $yy (@yy) {
16+
printf "%02d-%02d-%02d%02d\n",
17+
scalar reverse($yy), $dd, scalar reverse($dd), $yy;
18+
}
19+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env perl6
2+
3+
# ch-1.p6 - Survivor
4+
#
5+
# Ryan Thompson <[email protected]>
6+
7+
sub MAIN( Int $N = 50 ) {
8+
my Int @ll = 0, |[1..$N].rotate;
9+
my Int $cur = 1;
10+
11+
@ll[$cur] = @ll[ @ll[$cur] ] and $cur = @ll[$cur] until @ll[$cur] == $cur;
12+
13+
say $cur;
14+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env perl6
2+
3+
# ch-2.p6 - Palindrome Dates (mm/dd/yyyy)
4+
#
5+
# Ryan Thompson <[email protected]>
6+
7+
for (<02 12 22> X (1..12.fmt('%02d'.flip.sort).flat -> $dd, $yy {
8+
say "{$yy.flip}-$dd-{$dd.flip}$yy";
9+
}
10+
11+
# Naive method
12+
#.say for (2000..2999).map({ $_.flip.comb(2), $_ }).flat.grep: &valid-date;
13+
14+
#| Return True if a mmddyyyy date is valid
15+
# This won't work in general; it works here because of the range (see blog)
16+
#sub valid-date( $m, $d, $y ) { 1 ≤ $m ≤ 12 and 1 ≤ $d ≤ 31; }

0 commit comments

Comments
 (0)