# The Invitation

Most of us played with toy blocks growing up. It was fun and you learned stuff. So what else can you do but rise to the challenge when a 3-year old exclaims, "Look, I made a square!", then pointing to a pile of blocks, "Can _you_ do it?"

# These Blocks

Just to play along, of course we'll be viewing these blocks in two dimensions. Depth now being disregarded, it turns out the pile has four different sizes of block: `1x1`, `1x2`, `1x3`, and `1x4`. The smallest one represents the area of a square, the other three are rectangular, and all differ by their width. Integers matching these four widths are used to represent the blocks in the input.

# This Square

Well, the kid made a `4x4` square from this pile, so you'll have to match that. Noticing the way they fit together, you realize the structure must be built in fours rows, one row at a time, where the blocks must be placed horizontally. With the known types of block, there are five types of row you could build:

* 1 four-unit block
* 1 three-unit block plus 1 one-unit bock (in either order)
* 2 two-unit blocks
* 1 two-unit block plus 2 one-unit blocks (in any order)
* 4 one-unit blocks

Amounts for all four of the block sizes in the pile will each vary from `0` to `16`. The total size of the pile will also vary from `0` to `16`. The order of rows is irrelevant. A valid square doesn't have to use up all the given blocks.

# Some Examples

Given `1, 3, 2, 2, 4, 1, 1, 3, 1, 4, 2` there are many ways you could construct a square. Here are three possibilities, as described by their four rows:

* 1 four-unit block
* 2 two-unit blocks
* 1 four-unit block
* 4 one-unit blocks
>
* 1 three-unit block plus 1 one-unit block
* 2 two-unit blocks
* 1 four-unit block
* 1 one-unit block plus 1 three-unit block
>
* 2 two-unit blocks
* 1 three-unit block plus 1 one-unit block
* 1 four-unit block
* 2 one-unit blocks plus 1 two-unit block
>

Given `1, 3, 2, 4, 3, 3, 2` there is no way to complete the task, as you could only build three rows of the correct length. The kid will not be impressed.

* 2 two-unit blocks
* 1 three-unit block plus 1 one-unit block
* 1 four-unit block
* (here only sadness)
>

# Input
```python
blocks ~ a random list of integers (1 <= x <= 4)
```
# Output
```python
True or False ~ whether you can build a square
```
# Enjoy!

If interested, I also have [this kata](https://www.codewars.com/kata/5cb7baa989b1c50014a53333) as well as [this other kata](https://www.codewars.com/kata/5cb5eb1f03c3ff4778402099) to consider solving.

Difficulty: EASY
Tags: Geometry

Data structures

Game theory

Ad-hoc
Skill types: Data structures

Starter code:
def build_square(blocks):
	

Solutions:
def build_square(blocks):
	for x in range(4):
		if 4 in blocks:
			blocks.remove(4)
		elif 3 in blocks and 1 in blocks:
			blocks.remove(3)
			blocks.remove(1)
		elif blocks.count(2) >= 2:
			blocks.remove(2)
			blocks.remove(2)
		elif 2 in blocks and blocks.count(1) >= 2:
			blocks.remove(2)
			blocks.remove(1)
			blocks.remove(1)
		elif blocks.count(1) >= 4:
			blocks.remove(1)
			blocks.remove(1)
			blocks.remove(1)
			blocks.remove(1)
		else:
			return False
	return True


def build_square(blocks):
	return blocks.count(4) + min(blocks.count(3), blocks.count(1)) + (blocks.count(1) - min(blocks.count(3), blocks.count(1))) / 4 + blocks.count(2) / 2 >= 4


def build_square(arr):
	c = 0

	def purge(*sizes):
		nonlocal c
		used = []
		try:
			for s in sizes:
				arr.remove(s)
				used.append(s)
			c += 1
		except:
			arr.extend(used)
	for _ in range(16):
		purge(4)
		purge(3, 1)
		purge(2, 2)
		purge(2, 1, 1)
		purge(1, 1, 1, 1)
	return c >= 4


def build_square(blocks):
	(x1, x2, x3, x4) = (blocks.count(n) for n in range(1, 5))
	b3 = min(x3, x1)
	b2 = min(x2 % 2, (x1 - b3) // 2)
	b1 = (x1 - b3 - b2 * 2) // 4
	return x4 + b3 + x2 // 2 + b2 + b1 > 3


def build_square(sq):
	A = lambda x: all((sq.count(i) >= x.count(i) for i in x))
	for _ in range(4):
		t = next(([sq.pop(sq.index(l)) for l in k] for k in [[4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]] if A(k)), 0)
		if not t:
			return False
	return True


def build_square(blocks):
	a = 4
	b = blocks.count(1)
	c = blocks.count(2)
	d = blocks.count(3)
	e = blocks.count(4)
	a -= e
	if a <= 0:
		return True
	while b > 0 and d > 0:
		b -= 1
		d -= 1
		a -= 1
	while c > 0 and b > 1:
		b -= 2
		c -= 1
		a -= 1
	while b > 3:
		b -= 4
		a -= 1
	while c > 1:
		c -= 2
		a -= 1
	if a <= 0:
		return True
	else:
		return False


from collections import Counter

def build_square(blocks):
	c = Counter(blocks)
	n = 4
	n -= c[4]
	x = min(c[3], c[1])
	n -= x
	c[1] -= x
	x = c[2] // 2
	n -= x
	c[2] -= x * 2
	x = min(c[2], c[1] // 2)
	n -= x
	c[1] -= x * 2
	return c[1] // 4 >= n


build_square = lambda B: (lambda a, b, c, d: 15 < a + b * 2 + 3 * min(a, c) + d * 4)(*map(B.count, (1, 2, 3, 4)))


def build_square(blocks):
	nums = {i: blocks.count(i) for i in range(1, 5)}
	odd_comb = min(nums[3], nums[1])
	nums[1] -= odd_comb
	return nums[4] * 4 + odd_comb * 4 + nums[2] * 2 + nums[1] >= 16


build_square = lambda sq, A=lambda x, y: all((y.count(i) >= x.count(i) for i in x)): next((0 for _ in range(4) if not next(([sq.pop(sq.index(l)) for l in k] for k in [[4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1]] if A(k, sq)), 0)), 1)