7
\$\begingroup\$

You should write a program or function which given a text as input outputs or returns the sum of the following three integers:

  • Number of lowercase x's which are not part of a cross-formation.
  • Number of uppercase X's which are not part of a cross-formation.
  • Number of cross-formations which are made of neither x nor X.

A cross-formation is 5 copy of the same character positioned to each other in the following manner:

  A A
   A
  A A

Cross-formations can overlap with each other.

Detailed example

Input:
x-x+x+X++
-x-x+x++asd
x-x+x+X+

Info:
There is 1 x and 2 X's not in cross-formation and 
there are 2 cross-formations of +'s as shown here:
   + +X+ 
    +x+    
   + +X+

Output:
5

Input

  • A string containing only printable ascii characters (codepoint 32-126) and newlines.
  • The string will contain at least one printable character.

Output

  • A single positive integer, the sum of the number of occurrences for the three property.

Examples

After each output the values of the 3 properties (x's, X's and crosses) are provided in parens but note that those are not part of the output.

Input:
a
Output:
0 (0,0,0)

Input:
x xxx
 X x
x xxx
Output:
5 (4,1,0)

Input:
########
#####
########
Output:
4 (0,0,4)

Input:
a-a-a
0a0a
a-a-axXxX
0a0a
a-a-a
Output:
9 (2,2,5)

Input:
a c efghi
ab defgh
a c e ghij
abcd fgh
abc e g
Output:
2 (0,0,2)

Input:
/\/\/\/\/\/\/\
\/\/\/\/\/\/\/
#####x-x+x+X++
#####-x-x+x++###XX
#####x-x+x+X+
/\/\/\/\/\/\/\
\/\/\/\/\/\/\/
Output:
10 (1,4,5)

Input:
xxxxxxxxxx   
xxxxxxxXX
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxx
Output:
5 (3,2,0)

This is code-golf so the shortest entry wins.

\$\endgroup\$
  • \$\begingroup\$ Example 4 contains 2 cross formation of printable character codepoint 32 (space) that are not counted in output. Do spaces count? \$\endgroup\$ – edc65 May 8 '15 at 21:29
  • \$\begingroup\$ @edc65 i think space isnt allowed as counted for a cross , but i had similar question about if the line must be filled up with spaces until a newline , my work ll be based on positive assumption . \$\endgroup\$ – Abr001am May 8 '15 at 21:34
  • \$\begingroup\$ @edc65 Spaces do count, sorry about the incorrect example. :/ Corrected it. \$\endgroup\$ – randomra May 8 '15 at 21:39
  • \$\begingroup\$ so this s whole different thing , :S \$\endgroup\$ – Abr001am May 8 '15 at 21:44
  • \$\begingroup\$ @Agawa001 The lines are not necessary of equal lengths as you can see in the examples. \$\endgroup\$ – randomra May 8 '15 at 21:45
1
\$\begingroup\$

JavaScript (ES6) 234 246

Longer than I expected ...

Run snippet to test (in Firefox)

// Golfed

F=t=>t.split('\n').map((r,j,t)=>(
  [...r].map((c,i)=>(
    f=c==r[i+2]&c==u[i+1]&c==v[i]&c==v[i+2],
    c!='x'&c!='X'?z+=f
    :(++z,f?k[j+[,i]]=k[2+j+[,i]]=k[1+j+[,i+1]]=k[j+[,i+2]]=k[2+j+[,i+2]]=1:0)
  )),v=u,u=r),
k={},u=v=z=0)&&z-Object.keys(k).length

// Ungolfed 

U=t=>{
  var k={}, u=0,v=0,z=0, t=t.split('\n')
  t.forEach((r,j)=> {
    [...r].forEach((c,i)=> {
      x = c=='x'|c=='X' // is any X
      f = c==r[i+2] & c==u[i+1] & c==v[i] & c==v[i+2] // is in a formation
      if (x) 
      {
        ++z
        if (f)
        { // remember the position of X involved in a formation, avoid counting duplicates more tha once
          k[j+[,i]]=k[2+j+[,i]]=k[1+j+[,i+1]]=k[j+[,i+2]]=k[2+j+[,i+2]]=1 
        }
      }
      else
      {
        z+=f 
      }                                                  
    })
    v=u,u=r
  })
  return z-Object.keys(k).length
}

// TEST

Out=s=>OUT.innerHTML=OUT.innerHTML + s;

['a','a a\n a\na a','x xxx\n X x\nx xxx', 'x-x+x+X++\n-x-x+x++asd\nx-x+x+X+'
,'########\n#####\n########','a-a-a\n0a0a\na-a-axXxX\n0a0a\na-a-a'
,'a c efghi\nab defgh\na c e ghij\nabcd fgh\nabc e g'
,'/\\/\\/\\/\\/\\/\\/\\\n\\/\\/\\/\\/\\/\\/\\/\n#####x-x+x+X++\n#####-x-x+x++###XX\n#####x-x+x+X+\n/\\/\\/\\/\\/\\/\\/\\\n\\/\\/\\/\\/\\/\\/\\/'
,'xxxxxxxxxx\nxxxxxxxXX\nxxxxxxxxxx\nxxxxxxxxxx\nxxxxxxxxxx\nxxxxxxxx']
.forEach(t=>Out(t+'\n<b>'+F(t)+'</b>\n\n'))
b { color: #00f }
<pre id=OUT></pre>

\$\endgroup\$
1
\$\begingroup\$

C (562 b)


Golfed Code:

int main(){wchar_t *q,*y,*w,*b,a[50][50];int l,c,t,i,j,u,v,h,s,x,r;l=c=i=0;do l=(t=wcslen(q=_getws(a[i++])))>l?t:l;while(t);for(j=0;j/l<i-1;j++)c+=(x=((t=*(b=a[u=j/l]+(v=j%l)))==120|t==88|t==344|t==376))&(s=(((t&(h=0xff))!=(*(b+2)&h))|((t&h)!=(*(w=a[u+1]+(v+1))&h))|((t&h)!=(*(y=a[u+2]+v)&h))|((t&h)!=(*(y+2)&h))))?(t==(t&h)):(x?0*(*b=*(b+2)=*w=*y=*(y+2)=(t+256)):(((t&h)==(*(b+2)&h))&((*w&h)==(*y&h))&((*(y+2)&h)==(*y&h))&((*y&h)==(t&h))?(r=(t==(t&h))+(*(b+2)==(t&h))+(*w==(t&h))+(*y==(t&h))+(*(y+2)==(t&h)))+0*(*b=*(b+2)=*w=*y=*(y+2)=t+256):0));printf("%d",c);}

unpacked Code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(){
wchar_t *q,*y,*w,*b, a[50][50];int l,c,t,i,j,u,v,h,s,x,r;l=c=i=0;
do
l=(t=wcslen(q=_getws(a[i++])))>l?t:l;
while (t);
for(j=0;j/l<i-1;j++) 
c+=(x=((t=*(b=a[u=j/l]+(v=j%l)))==120|t==88|t==344|t==376 ))&(s=(((t&(h=0xff))!=(*(b+2)&h))|((t&h)!=(*(w=a[u+1]+(v+1))&h))|((t&h)!=(*(y=a[u+2]+v)&h))|((t&h)!=(*(y+2)&h))))?(t==(t&h)):(x?0*(*b=*(b+2)=*w=*y=*(y+2)=(t+256)):(((t&h)==(*(b+2)&h))&((*w&h)==(*y&h))&((*(y+2)&h)==(*y&h))&((*y&h)==(t&h))?(r=(t==(t&h))+(*(b+2)==(t&h))+(*w==(t&h))+(*y==(t&h))+(*(y+2)==(t&h)))+0*(*b=*(b+2)=*w=*y=*(y+2)=t+256):0));
printf("%d",c);getch();
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.