aboutsummaryrefslogtreecommitdiff
path: root/challenge-117/dave-jacoby
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-06-15 18:04:38 -0400
committerDave Jacoby <jacoby.david@gmail.com>2021-06-15 18:04:38 -0400
commitb8928c725f0ca2fb805c56fb7373c19d532d9e37 (patch)
tree36698a7f97c9eb2fe6dfa2a6f776564b9fd0742d /challenge-117/dave-jacoby
parentae1c7153d22d4b0d05b6d9fdc31b226f27a0d51f (diff)
downloadperlweeklychallenge-club-b8928c725f0ca2fb805c56fb7373c19d532d9e37.tar.gz
perlweeklychallenge-club-b8928c725f0ca2fb805c56fb7373c19d532d9e37.tar.bz2
perlweeklychallenge-club-b8928c725f0ca2fb805c56fb7373c19d532d9e37.zip
New Idea!
Diffstat (limited to 'challenge-117/dave-jacoby')
-rw-r--r--challenge-117/dave-jacoby/blog2.txt1
-rw-r--r--challenge-117/dave-jacoby/perl/ch-2b.pl40
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-117/dave-jacoby/blog2.txt b/challenge-117/dave-jacoby/blog2.txt
new file mode 100644
index 0000000000..172f98a91b
--- /dev/null
+++ b/challenge-117/dave-jacoby/blog2.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/06/15/perl-challenge-117-redux-permutations.html
diff --git a/challenge-117/dave-jacoby/perl/ch-2b.pl b/challenge-117/dave-jacoby/perl/ch-2b.pl
new file mode 100644
index 0000000000..2d1627875d
--- /dev/null
+++ b/challenge-117/dave-jacoby/perl/ch-2b.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ postderef say signatures state };
+no warnings qw{ experimental };
+
+use Carp;
+use Getopt::Long;
+use List::Util qw{ uniq };
+use Algorithm::Permute;
+
+my $n = 2;
+GetOptions( 'number=i' => \$n );
+croak 'Too Small' if $n < 0;
+
+my @solutions = solve_triangle($n);
+# say join ' ', ( scalar @solutions ), @solutions, ( scalar @solutions );
+
+sub solve_triangle ( $n ) {
+ my @output;
+ my $string = 'R' x $n;
+ push @output, $string;
+ my %hash;
+ my $c = 1;
+ while ( $string =~ /R/ ) {
+ $string =~ s/R/LH/;
+ my @list = split //, $string;
+ my $p = Algorithm::Permute->new( \@list );
+ while ( my @res = $p->next ) {
+ my $x = join '', @res;
+ next if $x =~ m{^H|L$};
+ # push @output, $x;
+ next if $hash{$x}++;
+ say join "\t", $c, $x ;
+ $c++;
+ }
+ }
+ return sort { length $b <=> length $a } uniq @output;
+}