You are sitting with your coworker, having lunch, and bragging to him/her about the latest and supposedly greatest project you've been working on. Getting sick and tired of your constant showcase of egoism, he/she gives you a challenge just so you'll shut up. Being the egoistic and happy-go-lucky person you are, you of course accept (because you must accept each and every challenge). The challenge, as he/she explains it is, given an input of a block of text containing 1 or more of each character in
!@#$^&*, output in any reasonable format the coordinates of the switch(es) that is/are "on".
According to your coworker, a switch is a
$, and a switch is classified as "on" if and only if it satisfies at least 1 of the following criteria:
It is surrounded by all
^^^ ^$^ ^^^
results in an "on" switch.
It is surrounded by all
&&& &$& &&&
results in an "on" switch.
It is completely covered on at least two sides with
*. For instance,
*** &$& ***
results in an "on" switch, but
&*& &$& &*&
does not, since the switch is not completely covered on any two sides by
There is at least 1
@in any of the corners around it. This does not count if either of these are not in a corner. So...
!&& ^$@ @&!
results in an "on" switch, since there is at least 1
@in at least 1 of the corners (in the above case, there are 2 valid
!s and 1 valid
@in 3 corners). And...
&!& ^$@ ^!&
does not, although there are 2
!s and 1
@, since none of them are in any of the corners.
1 or more
#are not on any sides around the switch, unless at least 1
&surrounds the switch. In other words, if there is at least 1
#present on a side, it overrides all other rules, unless there is also a
#&* *$* !**
results in an "on" switch, although a
#exists, since there is an
&around the switch, and it follows at least 1 of the above rules. However, if the exclamation point were not present like so:
#&* *$* ***
The switch would be off, since it does not follow at least one of the above rules. Therefore, even though a switch may be surrounded by a
&, it would still be off unless it follows one or more of these rules. Also, there must always be a >=1:1 ratio between
#s for the switch to be valid. For instance,
#&! *$* **#
would still be an invalid switch, although it follows 1 of these rules, since there are 2
#s, but only 1
&, and therefore not a >=1:1 ratio between
#s. To make this valid, you must add 1 or more additional
&s to any edge in order balance the number of
&s out, possibly like so:
#&! *$& &*# 3:2 ratio between &s and #s
#^^ ^$* @^!
results in an "off" switch, although it follows 1 or more of the above rules, since it contains at least 1
#around it, and no
&s to outbalance it.
The valid switches will only be inside an input, and therefore, each valid
$must be surrounded completely by any 8 of the valid characters. For instance, if the entire input were to be:
*$* !$! !!!
$is definitely not a valid switch since the switch is on an edge, and therefore the switch is not completely surrounded by 8 valid characters. In this instance, the switch should not even be considered. However, the switch in the middle is completely valid, and as a matter of fact is "on", since it meets at least one of the above requirements.
To demonstrate, consider this block of characters:
!@#^^$#!@ !@#$$*$&@ @$^!$!@&&
which we can label for coordinates like so, calling the vertical axis
y and the horizontal axis
y 3 !@#^^$#!@ 2 !@#$$*$&@ 1 @$^!$!@&& 123456789 x
The coordinates must always be returned in an
(x,y) format, similar to a two-dimensional coordinate grid. Now, which switches are on? Well, let's first find them all. We can already see that there is 1 in the very top row, and another in the very bottom. However, those are automatically no-ops, since they are not completely surrounded by 8 characters.
Next comes the one in row 2. Specifically, this one:
#^^ #$$ ^!$
We can see that there are 3
$ signs in this, but we just want to focus on the one in the middle, and, as you can probably see, it is already invalid, since it has 2
#s around it with no
&s to balance them out. Additionally, this does not even follow any of the rules, so even if it was a valid switch, it would be "off" anyways.
Next comes another one in row 2:
^^$ $$* !$!
Again, only focus on the switch in the middle. This switch is "on", since it has at least 1
! in at least 1 corner. The coordinates of this one are
Moving on, we finally move onto the last switch. This one is also in the second row and appears like so:
$#! *$& !@&
and, as you can probably see, this one is also a valid switch, although there is a
# surrounding it, since there are 2 other
&s to outbalance the
#. In addition to that, it also has at least 1
! in at least 1 of the corners, and therefore not only is the switch valid, but it's also "on". The coordinates of this switch are
We have finally reached the end, and have found 2 "on" switches in that entire block on text. Their coordinates are
(7,2), which is our final answer, and what the output should be. However, this input was very simple. Inputs can get a lot bigger, as there is not limit on how big the block of text can get. For instance, the input could even be a randomized
200x200 block of text.
Standard Loopholes are prohibited.
There can't possibly be a built-in for this, but just in case there is (looking at you Mathematica), the use of built-ins that directly solve this are prohibited.
Given in the format
string input -> [array output]:
@#$$&^!&!# @*&!!^$&^@ $!#*$@#@$! -> [[7,3],[9,2]] *@^#*$@&*# #^&!$!&$@@#&^^&*&*&& !^#*#@&^#^*$&!$!*^$$ #^#*#$@$@*&^*#^!^@&* -> [[19,3],[15,3],[8,2]] #$@$!#@$$^!#!@^@^^*# @!@!^&*@*@ *$*^$!*&#$ @$^*@!&&&# **$#@$@@#! -> [[2,8],[5,8],[6,6],[9,3]] ##*&*#!^&^ $&^!#$&^&@ ^^!#*#@#$* $@@&#@^!!& #@&#!$$^@$ !!@##!$^#!&!@$##$*$# $^*^^&^!$&^!^^@^&!#! @*#&@#&*$!&^&*!@*&** -> [[9,4],[9,3]] ^!!#&#&&&#*^#!^!^@!$ &$$^*$^$!#*&$&$#^^&$
More coming soon
- You can assume that the input will always be in the form of a complete block (i.e. a rectangle or square)
- There will never be any other character in the input than those in
Remember, this is a code-golf so the shortest code wins!