diff options
| author | Aaron Smith <asmith@sumologic.com> | 2020-11-28 20:23:05 -0600 |
|---|---|---|
| committer | Aaron Smith <asmith@sumologic.com> | 2020-11-28 20:23:19 -0600 |
| commit | f5fa5aa61c4f26e1446b20c7a94d436eb3aa77de (patch) | |
| tree | cae928d61dc53a71e4e4e0016ca11e67f07d0eb7 /challenge-088 | |
| parent | 72bc74ff75b5964cba369afe76c65b66bfb24bed (diff) | |
| download | perlweeklychallenge-club-f5fa5aa61c4f26e1446b20c7a94d436eb3aa77de.tar.gz perlweeklychallenge-club-f5fa5aa61c4f26e1446b20c7a94d436eb3aa77de.tar.bz2 perlweeklychallenge-club-f5fa5aa61c4f26e1446b20c7a94d436eb3aa77de.zip | |
Add Challenge 88 Solutions
Diffstat (limited to 'challenge-088')
| -rw-r--r-- | challenge-088/aaronreidsmith/README | 1 | ||||
| -rw-r--r-- | challenge-088/aaronreidsmith/raku/ch-1.raku | 9 | ||||
| -rw-r--r-- | challenge-088/aaronreidsmith/raku/ch-2.raku | 76 |
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-088/aaronreidsmith/README b/challenge-088/aaronreidsmith/README new file mode 100644 index 0000000000..b90b334a5a --- /dev/null +++ b/challenge-088/aaronreidsmith/README @@ -0,0 +1 @@ +Solution by Aaron Smith
\ No newline at end of file diff --git a/challenge-088/aaronreidsmith/raku/ch-1.raku b/challenge-088/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..90133cee74 --- /dev/null +++ b/challenge-088/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,9 @@ +#!/usr/bin/env perl6 + +subset PositiveInt of Int where { $_ ~~ Int && $_ > 0 } + +sub MAIN(*@N where all(@N) ~~ PositiveInt) { + my $product = [*] @N; + my @M = @N.map: $product / *; + say @M; +} diff --git a/challenge-088/aaronreidsmith/raku/ch-2.raku b/challenge-088/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..aaad86aedd --- /dev/null +++ b/challenge-088/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,76 @@ +#!/usr/bin/env perl6 + +subset PositiveInt of Int where { $_ ~~ Int && $_ > 0 } + +enum Direction <NORTH EAST SOUTH WEST>; + +sub MAIN(*@input where all(@input) ~~ PositiveInt) { + # Ensure our input is exactly square + my $side-length = @input.elems.sqrt; + $side-length.Int == $side-length or die "Must be a square matrix"; + + # Turn our CLI input into a list of lists (containing both the value and a flag for if we have visted it) + my @matrix = gather { + loop (my $i = 0; $i < @input.elems; $i += $side-length) { + my @row = @input[$i..^$i + $side-length].map({ Hash.new('value', $_, 'visited', False) }); + take @row; + } + } + + # Output list and helper function for adding to it + my @output; + sub visit-cell($i, $j) { + my %cell = @matrix[$i][$j]; + if !%cell{'visited'} { + @output.push(%cell{'value'}); + } + @matrix[$i][$j]{'visited'} = True; + } + + # Control vars used below + my ($min-row, $min-col) = 0, 0; + my ($max-row, $max-col) = @matrix.elems - 1, @matrix.tail.elems - 1; + my ($current-row, $current-col, $current-direction) = $min-row, $min-col, EAST; + + # Iterate through matrix in the given directions. Check if we are in a corner or if we have already + # visited the next cell to determine if we should turn + while @output.elems != @input.elems { + visit-cell($current-row, $current-col); + given $current-direction { + when EAST { + if $current-col == $max-col || @matrix[$current-row][$current-col+1]{'visited'} { + $current-direction = SOUTH; + $current-row += 1; + } else { + $current-col += 1; + } + } + when SOUTH { + if ($current-row == $max-row && $current-col == $max-col) || @matrix[$current-row+1][$current-col]{'visited'} { + $current-direction = WEST; + $current-col -= 1; + } else { + $current-row += 1; + } + } + when WEST { + if $current-col == $min-col || @matrix[$current-row][$current-col-1]{'visited'} { + $current-direction = NORTH; + $current-row -= 1; + } else { + $current-col -= 1; + } + } + when NORTH { + # No need to check for special case here, because we always start in the top left + if @matrix[$current-row-1][$current-col]{'visited'} { + $current-direction = EAST; + $current-col += 1; + } else { + $current-row -= 1; + } + } + } + } + say @output; +} |
