Skip to main content
Commonmark migration
Source Link

Perl 6, 41 38 37 bytes

3 bytes saved thanks to @nwellnhof.

1 byte saved thanks to Jo King.

{map {[+] ^∞Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!

##Explanation

Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

Perl 6, 41 38 37 bytes

3 bytes saved thanks to @nwellnhof.

1 byte saved thanks to Jo King.

{map {[+] ^∞Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!

##Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

Perl 6, 41 38 37 bytes

3 bytes saved thanks to @nwellnhof.

1 byte saved thanks to Jo King.

{map {[+] ^∞Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!

Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

added 37 characters in body
Source Link
Ramillies
  • 2k
  • 12
  • 17

Perl 6, 41 38 3837 bytes

3 bytes saved thanks to @nwellnhof.

1 byte saved thanks to Jo King.

{map {[+] ^∞ Z*^∞Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!Try it online!

##Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

Perl 6, 41 38 bytes

3 bytes saved thanks to @nwellnhof.

{map {[+] ^∞ Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!

##Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

Perl 6, 41 38 37 bytes

3 bytes saved thanks to @nwellnhof.

1 byte saved thanks to Jo King.

{map {[+] ^∞Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!

##Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

added 948 characters in body
Source Link
Ramillies
  • 2k
  • 12
  • 17

Perl 6, 4141 38 bytes

3 bytes saved thanks to @nwellnhof.

{map {[+] ^∞ Z* !<<[Z~~] $_<<.&[Z~~]},$_,[Z] $_.&[Z]} 

Try it online!Try it online!

#Explanation##Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<[Z~~] $_<<.&[Z~~]} on the input and on its transpose. [Z~~].&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

Perl 6, 41 bytes

{map {[+] ^∞ Z* !<<[Z~~] $_},$_,[Z] $_} 

Try it online!

#Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<[Z~~] $_} on the input and on its transpose. [Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Perl 6, 41 38 bytes

3 bytes saved thanks to @nwellnhof.

{map {[+] ^∞ Z*!<<.&[Z~~]},$_,.&[Z]} 

Try it online!

##Explanation

It takes the input as a list of lists of characters and returns list of length 2 containing zero-based X and Y coordinates of the needle.

It works by applying the block {[+] ^∞ Z* !<<.&[Z~~]} on the input and on its transpose. .&[Z~~] goes through all columns of the argument and returns True if all the elements are the same, False otherwise. We then negate all the values (so we have a list with one bool per column, where the bool answers the question "Is the needle in that column?"), multiply them element-wise with a sequence 0,1,2,... (True = 1 and False = 0) and sum the list, so the result of the whole block is the 0-based number of the column where the needle was found.

Nwellnhof's better approach, Perl 6, 34 bytes

{map *.first(:k,*.Set>1),.&[Z],$_} 

Try it online!

##Explanation

Generally the same approach, just more effective. It still uses a block on the array and its transpose, but now the block converts all rows intoSets and checks for the number of elements. The first function then gives index (due to the :k) of the first row that contained more than 1 element. Because of that, the order of $_ and .&[Z] needed to be swapped.

Source Link
Ramillies
  • 2k
  • 12
  • 17
Loading