Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active February 6, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

@AndyGaskell
Copy link

AndyGaskell commented Jul 3, 2016

Language: php7
Length: 178
Solution:

function s($a){foreach($a AS$x=>$c){$y=array_search("x",$c);if(is_int($y)){if(isset($r)){return array();}else{$r = array($x,$y);}}}if(isset($r)){return $r;}else{return array();}}

...four.... thought I'd tee off first :) First shot was a lot shorter, but only met half the test cases.

Copy link

ghost commented Jul 4, 2016

Language: JavaScript
Length: 94 122
Solution:

Original attempt (94)

_=i=>{
f=i.filter(x=>x.includes('x'))
$=f[0]
return f.length!=1?[]:[i.indexOf($),$.indexOf('x')]}

After realising to above won't work with two 'x''x on the same row.

_=i=>{
f=(``+i).replace(/,/g,'')
if((f.match(/x/g)||[]).length!=1)return[]
x=f.indexOf('x'),$=i[0].length
return[~~(x/$),x%$]
}

@billythekid
Copy link

billythekid commented Jul 4, 2016

Language: PHP
Length: 190 117 120
Solution:

function x($a){$o=[];array_filter($a,function($v,$k)use(&$o){if(empty($o)&&in_array('x',$v)){$o[]=[$k,array_flip($v)['x']];}elseif(!empty($o)&&in_array('x',$v)){$o=[];}},1);return$o[0]??[];}

...this is basically Andy's but refactored a little, for 117:

function x($a){foreach($a as$k=>$v){$y=array_search('x',$v);if($y!==false){$r=(isset($r))?[]:[$k,$y];}}return$r??[];}

…gah! fails on this array (should give [] as 2 x's on the same row, but gave [4,3]), so even though it passes the tests in the rules, it doesn't really follow the rules

[
        ['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', 'x', 'a', 'o', 'x', 'o'],
        ['d', 'o', 'o', 'o', 'o', 'o', 'o', 'o'],
]

working version for 120:

function x($a){foreach($a as$k=>$v){$y=array_keys($v,'x');if(count($y)==1){$r=(isset($r))?[]:[$k,$y[0]];}}return$r??[];}

@molarmanful
Copy link

molarmanful commented Jul 4, 2016

Language: Javascript
Length: 83
Solution:

x=>x.map((a,b)=>~(i=a.indexOf('x'))&&c.push(b,i),c=[])&&2 in c||/x.*x/.exec(x)?[]:c

@BaliBalo
Copy link

BaliBalo commented Jul 8, 2016

Language: Javascript
Length: 80 78
Solution:

a=>!(m=(r=/x/g).exec(a))||r.exec(a)?[]:[~~((i=m.index/2)/(l=a[0].length)),i%l]

Alternative longer versions :

  • 87

    a=>~(i=(s=a+'')[f='indexOf']('x'))&&!~s[f]('x',i+1)?[~~((i/=2)/(l=a[0].length)),i%l]:[]
  • 89

    a=>(i=(a+'').replace(/([^x]*)x[^x]*/,(m,v)=>v.length)/2)+1?[~~(i/(l=a[0].length)),i%l]:[]

@strayobject
Copy link

strayobject commented Aug 1, 2016

Language: PHP
Length: 114
Solution:

function x($a){foreach($a as$k=>$v){count($y=array_keys($v,'x'))==1?$r=(isset($r))?[]:[$k,$y[0]]:1;}return$r??[];}

Shortened version of solution by @billythekid

@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