6 kyu

Playing With Toy Blocks ~ Can you build a 4x4 square?

89 of 759rowcased

Description:

An 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?"

Our 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.

The 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. 1 four-unit block
  2. 1 three-unit block plus 1 one-unit bock (in either order)
  3. 2 two-unit blocks
  4. 1 two-unit block plus 2 one-unit blocks (in any order)
  5. 4 one-unit blocks

Some Notes

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

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:

ONE

  1. 1 four-unit block
  2. 2 two-unit blocks
  3. 1 four-unit block
  4. 4 one-unit blocks

TWO

  1. 1 three-unit block plus 1 one-unit block
  2. 2 two-unit blocks
  3. 1 four-unit block
  4. 1 one-unit block plus 1 three-unit block

THREE

  1. 2 two-unit blocks
  2. 1 three-unit block plus 1 one-unit block
  3. 1 four-unit block
  4. 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.

NONE

  1. 1 four-unit block
  2. 2 two-unit blocks
  3. 1 three-unit block plus 1 one-unit block
  4. (here only sadness)

Input

blocks ~ array of random integers (1 <= x <= 4)

Output

true or false ~ whether you can build a square

Enjoy!

...and please consider one of the following kata to solve next:

Logic
Arrays
Games
Puzzles
Geometry
Algorithms

Stats:

CreatedApr 8, 2019
PublishedJul 16, 2019
Warriors Trained2637
Total Skips72
Total Code Submissions5430
Total Times Completed759
C Completions89
Python Completions288
C# Completions94
JavaScript Completions171
Java Completions104
C++ Completions69
CoffeeScript Completions7
TypeScript Completions18
Haskell Completions3
Total Stars64
% of votes with a positive feedback rating95% of 155
Total "Very Satisfied" Votes138
Total "Somewhat Satisfied" Votes17
Total "Not Satisfied" Votes0
Total Rank Assessments9
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • rowcased Avatar
  • Blind4Basics Avatar
  • hobovsky Avatar
  • RobsonMoon Avatar
  • tobeannouncd Avatar
  • LosBlobbos Avatar
Ad