about summary refs log tree commit diff stats
path: root/algorithms
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-11-18 16:43:05 +0530
committerAndinus <andinus@nand.sh>2021-11-18 16:43:05 +0530
commit03745f7ce6b53054560f1c35d256de00c1113fd8 (patch)
treeda6133c620d95715c56e761fe23df7ba55a079d2 /algorithms
parent71ff6400dc3aef9ceb6c71f0d102f5b9ff40bab8 (diff)
downloadfornax-03745f7ce6b53054560f1c35d256de00c1113fd8.tar.gz
Copy neighbors subroutine
Diffstat (limited to 'algorithms')
-rw-r--r--algorithms/raku/DFS.raku59
1 files changed, 57 insertions, 2 deletions
diff --git a/algorithms/raku/DFS.raku b/algorithms/raku/DFS.raku
index 4789ce9..0cc7207 100644
--- a/algorithms/raku/DFS.raku
+++ b/algorithms/raku/DFS.raku
@@ -1,5 +1,3 @@
-use Octans::Neighbors;
-
 subset File of Str where *.IO.f;
 
 # Cells as defined by fornax format.
@@ -67,3 +65,60 @@ sub dfs(
         }
     }
 }
+
+# neighbors returns the neighbors of given index. Neighbors are cached
+# in @neighbors array. This way we don't have to compute them
+# everytime neighbors subroutine is called for the same position. We
+# don't need this caching here since every cell will be visited only
+# once. This subroutine was taken from Octans::Neighbors.
+sub neighbors(
+    @puzzle, Int $y, Int $x --> List
+) is export {
+    # @directions is holding a list of directions we can move in. It's
+    # used later for neighbors subroutine.
+    state List @directions = (
+        # $y, $x
+        ( +1, +0 ), # bottom
+        ( -1, +0 ), # top
+        ( +0, +1 ), # left
+        ( +0, -1 ), # right
+    );
+
+    # @neighbors holds the neighbors of given position.
+    state Array @neighbors;
+
+    if @puzzle[$y][$x] {
+        # Don't re-compute neighbors.
+        unless @neighbors[$y][$x] {
+            # Set it to an empty array because otherwise if it has no
+            # neighbors then it would've be recomputed everytime
+            # neighbors() was called.
+            @neighbors[$y][$x] = [];
+
+            my Int $pos-x;
+            my Int $pos-y;
+
+            # Starting from the intital position of $y, $x we move to
+            # each direction according to the values specified in
+            # @directions array. In this case we're just trying to
+            # move in 4 directions (top, bottom, left & right).
+            direction: for @directions -> $direction {
+                $pos-y = $y + $direction[0];
+                $pos-x = $x + $direction[1];
+
+                # If movement in this direction is out of puzzle grid
+                # boundary then move on to next direction.
+                next direction unless @puzzle[$pos-y][$pos-x];
+
+                # If neighbors exist in this direction then add them
+                # to @neighbors[$y][$x] array.
+                push @neighbors[$y][$x], [$pos-y, $pos-x];
+            }
+        }
+    } else {
+        # If it's out of boundary then return no neighbor.
+        @neighbors[$y][$x] = [];
+    }
+
+    return @neighbors[$y][$x];
+}
n Ser <contact@emersion.fr> 2019-04-27 15:09:59 +0000 committer Drew DeVault <sir@cmpwn.com> 2019-04-27 11:42:12 -0400 widgets/spinner: fix Spinner.frame race' href='/akspecs/aerc/commit/widgets/spinner.go?h=0.2.1&id=2159eb876e7e04e81f65e64b1d742ad832890289'>2159eb8 ^
a782b70 ^






0f8b7a1 ^



2159eb8 ^

a782b70 ^



2159eb8 ^
a782b70 ^


0f8b7a1 ^
de36484 ^
0f8b7a1 ^

2159eb8 ^

a782b70 ^

2159eb8 ^
a782b70 ^

a782b70 ^
5685a17 ^
a782b70 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89