Post History
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...
#4: Post edited
- # [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
# [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);exit1if$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 moveexit 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
# [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
# [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"
