How many squares are there?

12

1

This challenge is inspired by a picture that often roams on Facebook that looks like this. Except our base square will look more like this:

┌─┬───┬─┐
├─┼─┬─┼─┤
├─┼─┴─┼─┤
├─┼─┬─┼─┤
└─┴─┴─┴─┘

The square is made out of n x m 1x1 square, you have to count how many sub-squares (1x1, 2x2, 3x3, 4x4, 5x5, etc.) can fit within that square. Squares can be missing some grid lines (like in the example above) or be complete like in the example bellow. Which means a mathematical breakdown is not possible (as far as I know).

Inputs:

  • The amount of lines (n) of input to build the square;
  • A square made from the following characters: | across n lines of input.

Output:

  • The amount of squares of any size that can fit within the input square (we only want a single number here, not a number for each size).

Winning criterion:

The smallest answer (number of bytes) wins.

Test Cases:

In:

5
┌─┬─┬─┬─┐
├─┼─┼─┼─┤
├─┼─┼─┼─┤
├─┼─┼─┼─┤
└─┴─┴─┴─┘

Out: 30


In:

3
┌─┬─┐
├─┼─┤
└─┴─┘

Out: 5


In:

5
┌─┬─┐
├─┴─┤
├───┤
├─┬─┤
└─┴─┘

Out: 7


In:

4
┌─┬─┬─┬─┬─┬─┐
├─┼─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┼─┤
└─┴─┴─┴─┴─┴─┘

Out: 32


In:

2
┌─┐
└─┘

Out: 1


In:

4
┌─┬─┬─┬─┬─┬─┐
├─┴─┼─┼─┼─┴─┤
├─┬─┼─┼─┼─┬─┤
└─┴─┴─┴─┴─┴─┘

Out: 22

Simon Landry

Posted 2016-05-15T08:33:55.180

Reputation: 441

3I didn't count the bigger ones, but doesn't the third one have 11 squares in it? – Value Ink – 2016-05-15T08:37:35.053

@KevinLau-notKenny You're right I made a mistake. – Simon Landry – 2016-05-15T08:39:11.503

I think is too simple, it is counted via a combinatoric form, would you rather prefer to consider the picture format of facebook? – Abr001am – 2016-05-15T08:45:38.090

Is the input always an nxm rectangle? (If so, can we just take m and n as input, rather than the height + ASCII art?) – Sp3000 – 2016-05-15T08:50:06.437

@Agawa001 I'm not sure what you mean by combinatoric, but looking at the 1st test case, there are 16 1x1 squares, 9 2x2 squares, 4 3x3 squares and 1 4x4 square for a total of 30 sub squares. Unless I am completely missing something here, I don't think this is easy to come up with a concise answer. – Simon Landry – 2016-05-15T08:53:57.073

@Sp3000 you can ignore the first line of input in your program if you want, but you cannot change the rest of the input. – Simon Landry – 2016-05-15T08:57:39.140

@Sp3000 Alright learned a new word today, and yes, not saying it is impossible, just not too simple. – Simon Landry – 2016-05-15T09:02:30.223

Actually, I should probably ask to be doubly clear: is the input always an n by m rectangle of unit squares? Because if there can be missing gridlines or other shapes then the problem does get harder. – Sp3000 – 2016-05-15T09:02:53.943

@Sp3000 I guess I'll make it so we can be missing gridlines. It seems more interesting of a challenge. I originally intended for it to be a normal rectangle (n by m), but now that you mention it, it shouldn't be too hard to remove some grid lines to make the challenge harder. – Simon Landry – 2016-05-15T09:08:18.457

1

For reference, the rectangular case is A271916, which gives m*(m+1)*(3*n-m+1)/6 for an m by n rectangle with n >= m (dimensions offset by one since the entry speaks of points rather than the squares themselves)

– Sp3000 – 2016-05-15T09:20:23.173

1@SimonLandry i didnt mean combinatorics in pure sens, i think sp3000 just pointed that out already, the first version of your puzzle (before edit) was open for a simple mathematical breakthrough – Abr001am – 2016-05-15T09:44:32.420

@SimonLandry There are at least three different encodings of box drawing characters. Does it matter which encoding we use for the input? For example, in the list of inputs, | is the keyboard character having code 124. All the others, are the unicode box-drawing-characters starting at code point U+2500.

– RootTwo – 2016-05-16T20:31:04.290

@RootTwo Use whichever encoding you want as input as long as it is visually similar :) – Simon Landry – 2016-05-16T20:35:46.890

Answers

2

JavaScript (ES6), 292 bytes 306 325

Edit I did the byte count totally wrong, corrected now thx http://bytesizematters.com/ correct for the last time I hope thx Cᴏɴᴏʀ O'Bʀɪᴇɴ see https://goo.gl/LSHC1U (and 1 byte less using a literal newline insteads of '\n')

(h,z)=>(o=>{r=p=>" ),┌(─┐┬'└│├┘┴┤┼".search(z[p]);for(q=s=0;++s<o/2&s<h;)for(y=0;y<(h-s)*o;y+=o)for(x=0;x<o-s*2;q+=!n,x+=2)for(n=i=0,t=x,u=y;i<=s;t+=2,u+=o,i++)n|=i<s&(!(r(t+y)&r(t+y+s*o)&1)|!(r(x+u)&r(x+u+s*2)&2))|i>0&(!(r(t+y)&r(t+y+s*o)&4)|!(r(x+u)&r(x+u+s*2)&8))})(-~z.search`
`)|q

Longer than I expected (probably a few more byte can be shaved off)

All possible squares are checked and counted.

The r function map each character to a bitmap having

  • 1 : horizontal line center to right
  • 2 : vertical line center to bottom
  • 4 : horizontal line center to left
  • 8 : vertical line center to top

A square of any size must have

  • 4 in all cells except the first in top and bottom row
  • 1 in all cells except the last in top and bottom row
  • 8 in all cells except the first in leftmost and rightmost column
  • 2 in all cells except the last in leftmost and rightmost column

Test

f=(h,z)=>(o=>{r=p=>" ),┌(─┐┬'└│├┘┴┤┼".search(z[p]);k=(p,d,m)=>r(p)&r(p+s*d)&m;for(q=s=0;++s<o/2&s<h;)for(y=0;y<(h-s)*o;y+=o)for(x=0;x<o-s*2;q+=!n,x+=2)for(n=i=0,t=x,u=y;i<=s;t+=2,u+=o,i++)n=n|i<s&(!k(t+y,o,1)|!k(x+u,2,2))|i>0&(!k(t+y,o,4)|!k(x+u,2,8));})(-~z.search`
`)|q

console.log=(...x)=>O.textContent+=x+'\n'

// Less golfed

Uf=(h,z)=>{
  o=-~z.search`\n`;
  w=o/2;
  r=p=>" ),┌(─┐┬'└│├┘┴┤┼".search(z[p]);
  k=(p,d,m)=>r(p)&r(p+s*d)&m;
  for(q=s=0;++s<w&s<h;)
    for(y=0;y<(h-s)*o;y+=o)
      for(x=0;x<(w-s)*2;q+=!n,x+=2)
        for(n=i=0,t=x,u=y;i<=s;t+=2,u+=o,i++)
          n|=i<s&(!k(t+y,o,1)|!k(x+u,2,2))
          |i>0&(!k(t+y,o,4)|!k(x+u,2,8));
  return q
}

;[[5,`┌─┬───┬─┐
├─┼─┬─┼─┤
├─┼─┴─┼─┤
├─┼─┬─┼─┤
└─┴─┴─┴─┘`,20]
,[5,`┌─┬─┬─┬─┐
├─┼─┼─┼─┤
├─┼─┼─┼─┤
├─┼─┼─┼─┤
└─┴─┴─┴─┘`,30]
,[3,`┌─┬─┐
├─┼─┤
└─┴─┘`,5]
,[5,`┌─┬─┐
├─┴─┤
├───┤
├─┬─┤
└─┴─┘`,7]
,[4,`┌─┬─┬─┬─┬─┬─┐
├─┼─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┼─┤
└─┴─┴─┴─┴─┴─┘`,32]
,[2,`┌─┐
└─┘`,1]
,[4,`┌─┬─┬─┬─┬─┬─┐
├─┴─┼─┼─┼─┴─┤
├─┬─┼─┼─┼─┬─┤
└─┴─┴─┴─┴─┴─┘`,22],
,[6,`┌─┬─────┐
├─┼─┬─┐ │
│ ├─┼─┼─┤
│ └─┼─┼─┤
│   └─┼─┤
└─────┴─┘`,12],  
,[6,`┌─┬─┬─┬─┐
├─┴─┼─┼─┤
│   └─┼─┤
├─┬─┬─┼─┤
├─┼─┼─┼─┤
└─┴─┴─┴─┘`,23]]  
.forEach(t=>{
  var r=t[0],a=t[1],k=t[2],x=f(r,a)
  console.log(x==k?'OK '+x:'KO '+x+' Expected '+k,'\n'+a)
})
<pre id=O></pre>

edc65

Posted 2016-05-15T08:33:55.180

Reputation: 31 086

I count 307 bytes.

– Conor O'Brien – 2016-05-15T18:11:12.910

@Conor Ok thanks for the link – edc65 – 2016-05-15T20:18:28.583