From a5c540608c469a2797262facb766e175b932f0e2 Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 19 Jan 2021 21:53:16 +0530 Subject: Re-structure for CPAN upload, include a dictionary file bin/octans calls lib/Octans/CLI.rakumod which has the MAIN subroutine. --- lib/Octans/Neighbors.rakumod | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/Octans/Neighbors.rakumod (limited to 'lib/Octans/Neighbors.rakumod') diff --git a/lib/Octans/Neighbors.rakumod b/lib/Octans/Neighbors.rakumod new file mode 100644 index 0000000..c6f1c00 --- /dev/null +++ b/lib/Octans/Neighbors.rakumod @@ -0,0 +1,53 @@ +unit module Octans::Neighbors; + +# 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. +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] { + + # If we've already computed the neighbors then no need to do + # it again. + unless @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]; +} -- cgit 1.4.1-2-gfad0