From 1dd7eedea237292a3fb563ecff37d004c3bbc772 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Wed, 16 Jun 2021 21:13:06 +0800 Subject: week 117 --- challenge-117/cheok-yin-fung/awk/ch-1.awk | 22 +++++++++++ challenge-117/cheok-yin-fung/perl/ch-1.pl | 1 + challenge-117/cheok-yin-fung/perl/ch-2.pl | 63 +++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 challenge-117/cheok-yin-fung/awk/ch-1.awk create mode 100644 challenge-117/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-117/cheok-yin-fung/perl/ch-2.pl diff --git a/challenge-117/cheok-yin-fung/awk/ch-1.awk b/challenge-117/cheok-yin-fung/awk/ch-1.awk new file mode 100644 index 0000000000..9d6264cbda --- /dev/null +++ b/challenge-117/cheok-yin-fung/awk/ch-1.awk @@ -0,0 +1,22 @@ +# The Weekly Challenge 117 +# Task 1 Missing Row +# Usage: awk -f 'ch-1.awk' < [text file] + +BEGIN { + max = 15 + separator = "," + for (i = 1; i <= max; i++) + arr[i] = "F" +} + +{ + y = index($0, separator) + x = substr($0, 1, y-1) + arr[x] = "T" +} + +END { + for (i = 1; i <= max; i++) + if (arr[i] == "F") + print i +} diff --git a/challenge-117/cheok-yin-fung/perl/ch-1.pl b/challenge-117/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..79f84cb296 --- /dev/null +++ b/challenge-117/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1 @@ +while(<>) {/^\s*(\d+)/; $arr{$1} = 1;} for (1..15) {print ($_, "\n") unless $arr{$_};} diff --git a/challenge-117/cheok-yin-fung/perl/ch-2.pl b/challenge-117/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..40909a2072 --- /dev/null +++ b/challenge-117/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/perl +# The Weekly Challenge 117 +# Task 2 Find Possible Paths +# Usage: ch-2.pl [size of triangle] +use strict; +use warnings; + +my $N = $ARGV[0]; + +die "Usage: ch-2.pl [size of triangle]\n" + unless $ARGV[0] && $ARGV[0] =~ /^\d+$/; + +my @newarr = (1); + +while (scalar @newarr <= $N) { + my @arr = @newarr; + @newarr = (1); + my $ord = scalar @arr; + for my $i (1.. $ord - 1) { + push @newarr, $arr[$i-1] + $arr[$i] + $newarr[$i-1]; + } + $newarr[$ord] = $arr[$ord-1] + $newarr[$ord-1]; +} + +print "Number of Paths: ", $newarr[-1]; +print "\n"; + +# * * * * * * * +sub ways { + my $K = $_[0]; + my $newstp = [ [''] ]; + + while (scalar @{$newstp} <= $K) { + my $stp = $newstp; + my $ord = scalar @{$stp}; + $newstp = [ [ 'L' x $ord ] ]; + for my $i (1.. $ord - 1) { + push @{$newstp->[$i]}, $stp->[$i-1]->[$_] . 'R' + for (0.. scalar $stp->[$i-1]->@* - 1 ); + push @{$newstp->[$i]}, $stp->[$i]->[$_] . 'L' + for (0.. scalar $stp->[$i]->@* - 1 ); + push @{$newstp->[$i]}, $newstp->[$i-1]->[$_] . 'H' + for (0.. scalar $newstp->[$i-1]->@* - 1 ); + } + + push @{$newstp->[$ord]}, $stp->[$ord-1]->[$_] . 'R' + for (0.. scalar @{$stp->[$ord-1]} - 1 ); + push @{$newstp->[$ord]}, $newstp->[$ord-1]->[$_] . 'H' + for (0.. scalar @{$newstp->[$ord-1]} - 1 ); + } + + return $newstp->[-1]; +} + + +if ($N <= 10) { + print join " ", @{ways($N)}; +} +else { + print "...Too many possible paths!"; +} +print "\n"; + -- cgit