Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active February 6, 2017 08:36
Show Gist options
  • Save CodeGolfScotland/caef684aa8ec9189ab49a127cd928bc3 to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/caef684aa8ec9189ab49a127cd928bc3 to your computer and use it in GitHub Desktop.
Code Golf July 2016

July 2016 - 'x' marks the spot

Task:

Given a two dimensional array, return the co-ordinates of 'x'.

The co-ordinates should be zero indexed.

The solution should pass the example cases provided.

You may use any programming language.

You should assume you will always get an array as input.

Example test cases:

'Return an empty array if input is an empty array' => []

[] 

'Return an empty array if no x found' => []

[
  ['o', 'o'],
  ['o', 'o']
]

'Return an empty array if more than one x found' => []

[
  ['x', 'o'],
  ['o', 'x']
]

'Return an empty array if more than one x found on the same line' => []

[
  ['x', 'x'],
  ['o', 'o']
]

'Return [0,0] when x at top left' => [0, 0]

[
  ['x', 'o'],
  ['o', 'o']
]

'Return [4,6] for the example below' => [4, 6]

[
  ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
  ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
  ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
  ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
  ['o', 'o', 'o', 'o', 'o', 'o', 'x', 'o'],
  ['o', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
]
 
'Return [4,6] for the example below' => [4, 6] 

[
  ['t', 't', 't', 'o', 'o', 'o', '~', 'o'],
  ['o', 'r', 'o', 'o', 'o', 'o', 't', 'o'],
  ['o', 'o', 'o', 's', 'o', 'o', 'o', 'o'],
  ['o', 'o', 'c', 'o', 'o', 'o', 'o', 'o'],
  ['o', 'o', 'a', 'a', 'a', 'o', 'x', 'o'],
  ['d', 'o', 'o', 'o', 'o', 'o', 'o', 'o']
]

About Code Golf Scotland

@f-marais
Copy link

f-marais commented Aug 4, 2016

Language: Perl6
Length: 97
Solution:

sub r(@a){my @s=(map {@a[$_].grep('x',:k)},@a.keys).grep(/^.$/,:kv);+@s==2??[@s[0],@s[1][0]]!![]}

@joaquinferrero
Copy link

Language: Perl 6
Length: 91
Solution:

sub r{my@s=(map {.grep("x",:k)},@^a).grep(/./,:kv);(@s==2&&@s[1]==1)??[@s[0],@s[1][0]]!![]}

Demo:

perl6 -e 'my @a = [ [ "o", "o", "o" ], [ "o", "o", "x" ] ]; say "Result: {r(@a).gist}"; sub r{my@s=(map {.grep("x",:k)},@^a).grep(/./,:kv);(@s==2&&@s[1]==1)??[@s[0],@s[1][0]]!![]}'
Result: [1 2]

@HDudzus
Copy link

HDudzus commented Feb 4, 2017

Just because it was so much fun solving it by composing functions. :)
Language: Haskell (Lambdabot)
Length: 135 100
Solution:

s=(\l->case l of[x]->l;_->[]).(zip[0..].map(elemIndices 'x')>=> \(a,l)->map((,)a)l)>=> \(a,b)->[a,b]

Using lists as monad, nice occasion for the Kleisli operator >=>. You will need to import Data.List and Control.Monad. So I declare this to be a solution for the lambdabot.

Because monadic composition (data flows from left to right) is used after normal composition (data flows from right to left) after monadic composition after normal composition, in this case I strongly prefer the use of the reverse Kleisli operator <=< to let the data flow consequently from right to left.

Using the reverse Kleisli operator the lambda expressions have to be put in braces. This makes the solution 2 characters longer. It is therefore one of the rare cases, when the much more elegant applicative style map.(,)<$>fst<*>snd is shorter than the corresponding lambda expression in braces (\(a,l)->map((,)a)l). All in all, using the Kleisli operator in reverse, here, is twice an improvement in elegance and readability, but the solution is one character longer.
Language: Haskell (Lambdabot)
Length: 101
Solution:

s=(\(a,b)->[a,b])<=<(\l->case l of[x]->l;_->[]).(map.(,)<$>fst<*>snd<=<zip[0..].map(elemIndices 'x'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment