about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-01-20 22:50:59 +0530
committerAndinus <andinus@nand.sh>2021-01-20 22:50:59 +0530
commitd744a939d05402d8cdcb5cfec3b2b3073849f8ba (patch)
tree7196a388519e875c08051d070a94321b227b8045 /lib
parent5da030439368ed417694486a9c5275167db1b988 (diff)
downloadoctans-d744a939d05402d8cdcb5cfec3b2b3073849f8ba.tar.gz
Allow the input puzzle to be of any size
It should still be a 2d grid but can have any number of grids, not
necessarily MxN. Even this is a valid input:

    a b c
    s d e r c

This input should be valid even when parsing the url. It will
certainly be valid when the input is a file.
Diffstat (limited to 'lib')
-rw-r--r--lib/Octans/CLI.rakumod21
-rw-r--r--lib/Octans/Puzzle.rakumod58
2 files changed, 34 insertions, 45 deletions
diff --git a/lib/Octans/CLI.rakumod b/lib/Octans/CLI.rakumod
index 46b1afd..075a57f 100644
--- a/lib/Octans/CLI.rakumod
+++ b/lib/Octans/CLI.rakumod
@@ -29,22 +29,23 @@ multi sub MAIN (
     # @puzzle holds the puzzle.
     #
     # @gray-squares holds the list of indexes of valid starting
-    # positions in the puzzle. ($y, $x)
+    # positions in the puzzle.
     my (@puzzle, @gray-squares);
 
     # Set the sample puzzle if requested.
     if $sample {
-        parse-puzzle(
-            < n a t k
-              i m e c
-              a* r d e
-              t* e c h >,
-            @puzzle, @gray-squares
-        );
+        @puzzle = [
+            [<n a t k>],
+            [<i m e c>],
+            [<a* r d e>],
+            [<t* e c h>],
+        ];
     }
 
-    # Get the puzzle from $path if it's passed.
-    get-puzzle($_, @puzzle, @gray-squares) with $path;
+    # Get the puzzle from $path if it's passed. And set @gray-squares.
+    @puzzle = get-puzzle($_) with $path;
+    # set-gray-squares also removes asterisks from @puzzle.
+    @gray-squares = set-gray-squares(@puzzle); # ($y, $x)
 
     if $verbose {
         # Don't print path if using the dictionary included with the
diff --git a/lib/Octans/Puzzle.rakumod b/lib/Octans/Puzzle.rakumod
index 96a4870..89eae69 100644
--- a/lib/Octans/Puzzle.rakumod
+++ b/lib/Octans/Puzzle.rakumod
@@ -2,23 +2,15 @@ unit module Octans::Puzzle;
 
 use WWW;
 
-# get-puzzle returns the @puzzle along with it's @gray-squares.
+# get-puzzle returns the @puzzle given input path.
 sub get-puzzle (
-    Str $path,
-
-    # @puzzle will hold the puzzle grid.
-    @puzzle,
-
-    # @gray-squares will hold the position of gray squares. Algot
-    # marks them with an asterisk ("*") after the character.
-    @gray-squares
+    Str $path
 ) is export {
-    # @raw_puzzle will hold the puzzle before parsing.
-    my @raw-puzzle;
+    my @puzzle;
 
     # Read the puzzle from file if it exists.
     if $path.IO.f {
-        @raw-puzzle = $path.IO.lines.words;
+        @puzzle = $path.IO.lines.map(*.words.cache.Array);
     } else {
         # $url will hold the url that we'll call to get the toot data.
         my Str $url;
@@ -37,36 +29,32 @@ sub get-puzzle (
         if (jget($url)<media_attachments>[0]<description> ~~
 
             # This regex gets the puzzle in $match.
-            / [[(\w [\*]?) \s*] ** 4] ** 4 $/) -> $match {
-
-            @raw-puzzle = $match[0];
+            / ([(\w [\*]?) \s*?]+ \n)+  $/) -> $match {
+            for 0 .. $match[0].end -> $y {
+                for 0 .. $match[0][$y].words.end -> $x {
+                    @puzzle[$y][$x] = $match[0][$y].words[$x].lc;
+                }
+            }
         }
     }
-    parse-puzzle(@raw-puzzle, @puzzle, @gray-squares);
+    return @puzzle;
 }
 
-# parse-puzzle parses the puzzle from @raw-puzzle. It's assumed to be
-# a 4x4 grid.
-sub parse-puzzle (
-    @raw-puzzle, @puzzle, @gray-squares
+# set-gray squares will set the @gray-squares array while removing the
+# "*" in @puzzle. Algot marks them with an asterisk ("*") after the
+# character.
+sub set-gray-squares (
+    @puzzle --> List
 ) is export {
-    # @gray-squares should be empty.
-    @gray-squares = ();
-
-    # We have each character of the puzzle stored in @raw-puzzle.
-    for 0 .. 3 -> $y {
-        for 0 .. 3 -> $x {
-            with @raw-puzzle[($y * 4) + $x].Str.lc -> $char {
+    my List @gray-squares;
 
-                # If it ends with an asterisk then we push the
-                # position to @gray-squares.
-                if $char.ends-with("*") {
-                    @puzzle[$y][$x] = $char.comb[0];
-                    push @gray-squares, [$y, $x];
-                } else {
-                    @puzzle[$y][$x] = $char;
-                }
+    for 0 .. @puzzle.end -> $y {
+        for 0 .. @puzzle[$y].end -> $x {
+            if @puzzle[$y][$x].ends-with("*") {
+                @puzzle[$y][$x] .= chop;
+                push @gray-squares, ($y, $x);
             }
         }
     }
+    return @gray-squares;
 }