Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges 5 by 5 knight's tour string validator

Perl 5, 93 bytes I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢 Reads a string from stdin. Exits 0 if vali...

posted 4mo ago by jhnc‭  ·  edited 3mo ago by jhnc‭

Answer
#4: Post edited by user avatar jhnc‭ · 2026-06-12T16:29:46Z (3 months ago)
why did I think grouping assignments was shorter than doing individually?! borrow m90 idea
  • # [Perl 5], 93 bytes
  • I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢
  • Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.
  • -6 bytes from m90.
  • ```
  • perl -ne'map{$_=ord;$x=$_%5;$y=int$_/5;exit 1if$i++&&2-abs(($x-$X)*($y-$Y));$X=$x;$Y=$y}/./g'
  • ```
  • ```
  • echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
  • # walk the positions
  • map {
  • # convert character to ascii ordinal value.
  • # conveniently A is 65 which is a multiple of 5
  • # so no offset is needed for modulo to work
  • $_ = ord;
  • # derive x/y coordinates
  • $x = $_%5;
  • $y = int $_/5;
  • # after the first move, abort if not a legal move
  • # a legal move differs by 1 or 2 in x and 2 or 1 in y
  • # from the previous position
  • # borrowing @m90's observation, if we multiply together
  • # we just need to for check +/- 2
  • exit 1 if $i++ && 2 - abs(($x-$X)*($y-$Y));
  • # current position becomes previous position
  • $X = $x;
  • $Y = $y
  • } /./g
  • '
  • ```
  • [Try it online!][TIO-mqb50ny9]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mqb50ny9]: https://tio.run/##dZBZS8NAFIXf51cM9aqtGoNLXzpEyNLs@9YEhFI11YAmIZNCi/SvW6cVgwq@3eW7Z86Zpmhfx7sj3BW0wy@LtiooRStaYLp6oPi0WJfdKUGswfsSv2NohcFy8UqL@2pA8HPd1fiprgq8RftN164OCwSlMBwR9PhSvzUENW1ZdUs8OOZubinm7vDgAuZMlseXZzxPdm@L5h3mQt0@EVgLMD8eE9gI7Abm/JgcXr4ql1Cen5@cXHOLBzocwpqDbHQ2hA0H@WhEIBNgTSAXYLPlL/nnv5kI2tucfFlhKZjFhlncOaasBpmnaP4sNiQrjfSpm4eJLeIJ3qdBop2EuTvVo9SSjHjma4qXBapsOt/Ar6ufcgw4/BT6R0C0e8I2JNOfubIaMynRS0JNcbIgt6Y94aqKFrKBHmS@KdvMLMOi1JF6IvR0K1Viw07k3NV8KXPMaDoTA7UndNbakZlJVugpqRq4cm7Ejq8lPfFRN11ZV3THVZ8 "Perl 5 – Try It Online"
  • # [Perl 5], 93 bytes
  • I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢
  • Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.
  • -6 bytes from @m90.
  • ```
  • perl -ne'map{$_=ord;$x=$_%5;$y=int$_/5;exit 1if$i++&&2-abs(($x-$X)*($y-$Y));$X=$x;$Y=$y}/./g'
  • ```
  • ```
  • echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
  • # walk the positions
  • map {
  • # convert character to ascii ordinal value.
  • # conveniently A is 65 which is a multiple of 5
  • # so no offset is needed for modulo to work
  • $_ = ord;
  • # derive x/y coordinates
  • $x = $_%5;
  • $y = int $_/5;
  • # after the first move, abort if not a legal move
  • # a legal move differs by 1 or 2 in x and 2 or 1 in y
  • # from the previous position
  • # borrowing @m90's observation, if we multiply together
  • # we just need to check for +/- 2
  • exit 1 if $i++ && 2 - abs(($x-$X)*($y-$Y));
  • # current position becomes previous position
  • $X = $x;
  • $Y = $y
  • } /./g
  • '
  • ```
  • [Try it online!][TIO-mqb50ny9]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mqb50ny9]: https://tio.run/##dZBZS8NAFIXf51cM9aqtGoNLXzpEyNLs@9YEhFI11YAmIZNCi/SvW6cVgwq@3eW7Z86Zpmhfx7sj3BW0wy@LtiooRStaYLp6oPi0WJfdKUGswfsSv2NohcFy8UqL@2pA8HPd1fiprgq8RftN164OCwSlMBwR9PhSvzUENW1ZdUs8OOZubinm7vDgAuZMlseXZzxPdm@L5h3mQt0@EVgLMD8eE9gI7Abm/JgcXr4ql1Cen5@cXHOLBzocwpqDbHQ2hA0H@WhEIBNgTSAXYLPlL/nnv5kI2tucfFlhKZjFhlncOaasBpmnaP4sNiQrjfSpm4eJLeIJ3qdBop2EuTvVo9SSjHjma4qXBapsOt/Ar6ufcgw4/BT6R0C0e8I2JNOfubIaMynRS0JNcbIgt6Y94aqKFrKBHmS@KdvMLMOi1JF6IvR0K1Viw07k3NV8KXPMaDoTA7UndNbakZlJVugpqRq4cm7Ejq8lPfFRN11ZV3THVZ8 "Perl 5 – Try It Online"
#3: Post edited by user avatar jhnc‭ · 2026-06-12T16:28:32Z (3 months ago)
why did I think grouping assignments was shorter than doing individually?! borrow m90 idea
  • # [Perl 5], 107 bytes
  • I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢
  • Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.
  • ```
  • perl -ne'map{$_=ord;($x,$y)=($_%5,int$_/5);$_=$x-$X.abs($y-$Y);exit
  • 1if$i++&&!/12|21/;($X,$Y)=($x,$y)}/./g'
  • ```
  • ```
  • echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
  • # walk the positions
  • map {
  • # convert character to ascii ordinal value.
  • # conveniently A is 65 which is a multiple of 5
  • # so no offset is needed for modulo to work
  • $_ = ord;
  • # calculate its x/y coordinates
  • ($x, $y) = ( $_%5, int $_/5 );
  • # a legal move differs by 1 or 2 in x and 2 or 1 in y
  • # from the previous position
  • # board has 5 rows/cols so any difference is single-digit
  • # due to operator precedence, doing abs here is shorter
  • # than testing for a minus below: /1-?2|2-?1/
  • $_ = $x-$X . abs($y-$Y);
  • # after the first move, abort if not a legal move
  • exit 1 if $i++ && !/12|21/;
  • # current position becomes previous position
  • ($X,$Y) = ($x,$y)
  • } /./g
  • '
  • ```
  • [Try it online!][TIO-mps06n9n]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mps06n9n]: https://tio.run/##dZBZS8NAFIXf51eMdVpbbRqq9sUhQpZm37cmIJSqaQ3YJGRSqKh/3TpxCSr4NnfOdw/n3CqrH2eHY9hkpIEPq7rICAE7kkGyuyXwJNvnzQkGdIDtEz5DVHO99eqRZDdFD8NN2ZTwviwy@Apapal3HwJAOTccYXD3UG4rDKo6L5o17PWZi0sCmWvYG6MltWXh5JRl8WG7qp7RkivrezxE@zF6GnFDtOzPxnQNLdnZCFMV7RmUTFa3ZIieGJSOcJsITPM1ys/OBoMjdnr@cj5lqUMypjL35fTKTtjN34YYtKGvPoPRTjRwRQMfLF2UvcSRFHcRaoIRB@rcTv3I5OEVbLsB3oz81J6rQWwIWrhwFclJPFnUrW/g19ZPOwp83A38Y8CbHWFqgu4ubFEOqRXvRL4iWYmXGvOOsGVJ8emH6iWuLpo0LMWC2BI6wndUI5ZCzYzE1FZcIbH0YL7gPbkjVDqagZ4Ihu9IsezZYqqFlqtEHfFWVk1eFuTAFO8 "Perl 5 – Try It Online"
  • # [Perl 5], 93 bytes
  • I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢
  • Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.
  • -6 bytes from m90.
  • ```
  • perl -ne'map{$_=ord;$x=$_%5;$y=int$_/5;exit 1if$i++&&2-abs(($x-$X)*($y-$Y));$X=$x;$Y=$y}/./g'
  • ```
  • ```
  • echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
  • # walk the positions
  • map {
  • # convert character to ascii ordinal value.
  • # conveniently A is 65 which is a multiple of 5
  • # so no offset is needed for modulo to work
  • $_ = ord;
  • # derive x/y coordinates
  • $x = $_%5;
  • $y = int $_/5;
  • # after the first move, abort if not a legal move
  • # a legal move differs by 1 or 2 in x and 2 or 1 in y
  • # from the previous position
  • # borrowing @m90's observation, if we multiply together
  • # we just need to for check +/- 2
  • exit 1 if $i++ && 2 - abs(($x-$X)*($y-$Y));
  • # current position becomes previous position
  • $X = $x;
  • $Y = $y
  • } /./g
  • '
  • ```
  • [Try it online!][TIO-mqb50ny9]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mqb50ny9]: https://tio.run/##dZBZS8NAFIXf51cM9aqtGoNLXzpEyNLs@9YEhFI11YAmIZNCi/SvW6cVgwq@3eW7Z86Zpmhfx7sj3BW0wy@LtiooRStaYLp6oPi0WJfdKUGswfsSv2NohcFy8UqL@2pA8HPd1fiprgq8RftN164OCwSlMBwR9PhSvzUENW1ZdUs8OOZubinm7vDgAuZMlseXZzxPdm@L5h3mQt0@EVgLMD8eE9gI7Abm/JgcXr4ql1Cen5@cXHOLBzocwpqDbHQ2hA0H@WhEIBNgTSAXYLPlL/nnv5kI2tucfFlhKZjFhlncOaasBpmnaP4sNiQrjfSpm4eJLeIJ3qdBop2EuTvVo9SSjHjma4qXBapsOt/Ar6ufcgw4/BT6R0C0e8I2JNOfubIaMynRS0JNcbIgt6Y94aqKFrKBHmS@KdvMLMOi1JF6IvR0K1Viw07k3NV8KXPMaDoTA7UndNbakZlJVugpqRq4cm7Ejq8lPfFRN11ZV3THVZ8 "Perl 5 – Try It Online"
#2: Post edited by user avatar jhnc‭ · 2026-05-30T07:05:35Z (4 months ago)
first abs() is not required since leading minus will be ignored in regex
  • # [Perl 5], 112 bytes
  • I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢
  • Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.
  • ```
  • perl -ne'map{$_=ord;($x,$y)=($_%5,int$_/5);$_=abs($x-$X).abs($y-$Y);exit
  • 1if$i++&&!/12|21/;($X,$Y)=($x,$y)}/./g'
  • ```
  • ```
  • echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
  • # walk the positions
  • map {
  • # convert character to ascii ordinal value.
  • # conveniently A is 65 which is a multiple of 5
  • # so no offset is needed for modulo to work
  • $_ = ord;
  • # calculate its x/y coordinates
  • ($x,$y) = ($_%5,int$_/5);
  • # a legal move differs by 1 or 2 in x and 2 or 1 in y
  • # from the previous position
  • $_ = abs($x-$X).abs($y-$Y);
  • # after the first move, abort if not a legal move
  • exit 1 if $i++ && !/12|21/;
  • # current position becomes previous position
  • ($X,$Y) = ($x,$y)
  • } /./g
  • '
  • ```
  • [Try it online!][TIO-mppj4qby]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mppj4qby]: https://tio.run/##dZBZS8NAFIXf51eMddRGm4aqfXGIkKXZ960JCKVqqgFNQiYFpfavWydVgwq@3Tvnu4dzps6bp@nuELY5aeHjsilzQsCa5JCsbwk8yV@K9gQDusBuhBuIGn6wWj6R/KYcYPhQtRW8r8ocbkGntM16LwBU8EMGg7vH6rnGoG6Ksl3BwRF7cUkgew0HI7Sgthwcn3Ic3j0v6w1a8FVzj4foZYReGX6IFkfTET1DC27KYKoubwkVWZQy4/34yqKMwV0sMClWqDg7Oz4@4Cbnb@cTjtqkIyrzX3Zbbsw9/K2JQZf86jMdLUZT1zT1zjYkxU9dWfXmkS6aSajNnCyILQFewa4gEKw4yJyZFiamqEdzT5Xd1Fckw/4Gfl39tKPA/vPAPwaC1ROWLhre3JGUiFoJbhyosp36mTnrCUeR1YA@aH7qGZJFw1IsTGyxJwJXMxM50q1YyhzVE1PbCGdzwVd6QqOrFRqpaAaunCi@I2V6ZHtq3BPvVd0WVUl2bPkB "Perl 5 – Try It Online"
  • # [Perl 5], 107 bytes
  • I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢
  • Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.
  • ```
  • perl -ne'map{$_=ord;($x,$y)=($_%5,int$_/5);$_=$x-$X.abs($y-$Y);exit
  • 1if$i++&&!/12|21/;($X,$Y)=($x,$y)}/./g'
  • ```
  • ```
  • echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
  • # walk the positions
  • map {
  • # convert character to ascii ordinal value.
  • # conveniently A is 65 which is a multiple of 5
  • # so no offset is needed for modulo to work
  • $_ = ord;
  • # calculate its x/y coordinates
  • ($x, $y) = ( $_%5, int $_/5 );
  • # a legal move differs by 1 or 2 in x and 2 or 1 in y
  • # from the previous position
  • # board has 5 rows/cols so any difference is single-digit
  • # due to operator precedence, doing abs here is shorter
  • # than testing for a minus below: /1-?2|2-?1/
  • $_ = $x-$X . abs($y-$Y);
  • # after the first move, abort if not a legal move
  • exit 1 if $i++ && !/12|21/;
  • # current position becomes previous position
  • ($X,$Y) = ($x,$y)
  • } /./g
  • '
  • ```
  • [Try it online!][TIO-mps06n9n]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mps06n9n]: https://tio.run/##dZBZS8NAFIXf51eMdVpbbRqq9sUhQpZm37cmIJSqaQ3YJGRSqKh/3TpxCSr4NnfOdw/n3CqrH2eHY9hkpIEPq7rICAE7kkGyuyXwJNvnzQkGdIDtEz5DVHO99eqRZDdFD8NN2ZTwviwy@Apapal3HwJAOTccYXD3UG4rDKo6L5o17PWZi0sCmWvYG6MltWXh5JRl8WG7qp7RkivrezxE@zF6GnFDtOzPxnQNLdnZCFMV7RmUTFa3ZIieGJSOcJsITPM1ys/OBoMjdnr@cj5lqUMypjL35fTKTtjN34YYtKGvPoPRTjRwRQMfLF2UvcSRFHcRaoIRB@rcTv3I5OEVbLsB3oz81J6rQWwIWrhwFclJPFnUrW/g19ZPOwp83A38Y8CbHWFqgu4ubFEOqRXvRL4iWYmXGvOOsGVJ8emH6iWuLpo0LMWC2BI6wndUI5ZCzYzE1FZcIbH0YL7gPbkjVDqagZ4Ihu9IsezZYqqFlqtEHfFWVk1eFuTAFO8 "Perl 5 – Try It Online"
#1: Initial revision by user avatar jhnc‭ · 2026-05-28T13:37:54Z (4 months ago)
# [Perl 5], 112 bytes

I am disappointed to discover that just doing the obvious coordinate calculations requires fewer bytes than my lookup table approach. 😢

Reads a string from stdin. Exits 0 if valid tour, 1 if invalid.

```
perl -ne'map{$_=ord;($x,$y)=($_%5,int$_/5);$_=abs($x-$X).abs($y-$Y);exit
1if$i++&&!/12|21/;($X,$Y)=($x,$y)}/./g'
```


```
echo 'MJCFQXODGPWTIBKVSHENYRULA' | perl -ne'
    # walk the positions
    map {
        # convert character to ascii ordinal value.
        # conveniently A is 65 which is a multiple of 5
        #   so no offset is needed for modulo to work
        $_ = ord;

        # calculate its x/y coordinates
        ($x,$y) = ($_%5,int$_/5);

        # a legal move differs by 1 or 2 in x and 2 or 1 in y
        #   from the previous position
        $_ = abs($x-$X).abs($y-$Y);

        # after the first move, abort if not a legal move
        exit 1 if $i++ && !/12|21/;

        # current position becomes previous position
        ($X,$Y) = ($x,$y)
    } /./g
'
```

[Try it online!][TIO-mppj4qby]

[Perl 5]: https://www.perl.org/
[TIO-mppj4qby]: https://tio.run/##dZBZS8NAFIXf51eMddRGm4aqfXGIkKXZ960JCKVqqgFNQiYFpfavWydVgwq@3Tvnu4dzps6bp@nuELY5aeHjsilzQsCa5JCsbwk8yV@K9gQDusBuhBuIGn6wWj6R/KYcYPhQtRW8r8ocbkGntM16LwBU8EMGg7vH6rnGoG6Ksl3BwRF7cUkgew0HI7Sgthwcn3Ic3j0v6w1a8FVzj4foZYReGX6IFkfTET1DC27KYKoubwkVWZQy4/34yqKMwV0sMClWqDg7Oz4@4Cbnb@cTjtqkIyrzX3Zbbsw9/K2JQZf86jMdLUZT1zT1zjYkxU9dWfXmkS6aSajNnCyILQFewa4gEKw4yJyZFiamqEdzT5Xd1Fckw/4Gfl39tKPA/vPAPwaC1ROWLhre3JGUiFoJbhyosp36mTnrCUeR1YA@aH7qGZJFw1IsTGyxJwJXMxM50q1YyhzVE1PbCGdzwVd6QqOrFRqpaAaunCi@I2V6ZHtq3BPvVd0WVUl2bPkB "Perl 5 – Try It Online"