aboutsummaryrefslogtreecommitdiff
path: root/challenge-076
diff options
context:
space:
mode:
authorJason Messer <jasoncmesser@gmail.com>2020-09-13 18:41:32 -0700
committerJason Messer <jasoncmesser@gmail.com>2020-09-13 18:41:32 -0700
commitfccd0d43fb6f1347187fa26c577e9f6854ee6cc3 (patch)
treec2d476a6f91600d8c74622f98a6255e51d8b30ec /challenge-076
parentd0dff42c33aa3fe309b3fc41253e4a72558917d5 (diff)
downloadperlweeklychallenge-club-fccd0d43fb6f1347187fa26c577e9f6854ee6cc3.tar.gz
perlweeklychallenge-club-fccd0d43fb6f1347187fa26c577e9f6854ee6cc3.tar.bz2
perlweeklychallenge-club-fccd0d43fb6f1347187fa26c577e9f6854ee6cc3.zip
Correct and simplify my solution for the week 76 challenge
Diffstat (limited to 'challenge-076')
-rw-r--r--challenge-076/jason-messer/raku/ch-2.p681
1 files changed, 6 insertions, 75 deletions
diff --git a/challenge-076/jason-messer/raku/ch-2.p6 b/challenge-076/jason-messer/raku/ch-2.p6
index 2bb26bdfd3..39985d4576 100644
--- a/challenge-076/jason-messer/raku/ch-2.p6
+++ b/challenge-076/jason-messer/raku/ch-2.p6
@@ -16,87 +16,18 @@ sub MAIN($dict) {
}
for ^@grid.elems -> $x {
for ^@grid[$x].elems -> $y {
- with cast_nw($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_n($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_ne($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_e($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_se($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_s($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_sw($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
- with cast_w($x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
+ for ( ((0,-1,1) X (0,-1,1))[1..*] ) -> $dir_offset {
+ with cast_ray($dir_offset,$x, $y).grep( {.chars >= 4} ) { .say if .elems > 0 }
+ }
}
}
}
-# (-1, -1) #nw
-sub cast_nw($x is copy, Int $y is copy) {
+sub cast_ray($dir, $x is copy, Int $y is copy) {
my @potential;
- my @found = gather loop (; $x > 0 and $y > 0; $x--, $y--) {
+ my @found = gather
+ loop (; (0 <= $x < @grid.elems) and (0 <= $y < @grid.first.elems); $x += $dir.first, $y += $dir.tail) {
@potential.append: lc(@grid[$x][$y]);
with @potential.join { .take if %dict{$_} }
}
}
-
-# (-1, 0), #n
-sub cast_n($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $x > 0; $x--) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-
-# (-1, 1), #ne
-sub cast_ne($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $x > 0 and $y < @grid.first.elems; $x--, $y++) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-
-# (0, 1), #e
-sub cast_e($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $y < @grid.first.elems; $y++) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-
-# (1, 1), #se
-sub cast_se($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $x < @grid.elems and $y > @grid.first.elems; $x++, $y++) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-
-# (1, 0), #s
-sub cast_s($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $x < @grid.elems; $x++) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-
-# (1, -1), #sw
-sub cast_sw($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $x < @grid.elems and $y > 0; $x++, $y--) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-
-# (0, -1), #w
-sub cast_w($x is copy, Int $y is copy) {
- my @potential;
- my @found = gather loop (; $y > 0; $y--) {
- @potential.append: lc(@grid[$x][$y]);
- with @potential.join { .take if %dict{$_} }
- }
-}
-