Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ slug.

| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
| 0 | 1 | 2 | 26 | 48 | 443 | 605 | 228 | 56 | 82 |
| 0 | 1 | 2 | 26 | 48 | 444 | 605 | 228 | 56 | 82 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
- [Playing on a chessboard](playing-on-a-chessboard)
- [Playing with digits](playing-with-digits)
- [Playing with passphrases](playing-with-passphrases)
- [Playing With Toy Blocks ~ Can you build a 4x4 square?](playing-with-toy-blocks-can-you-build-a-4x4-square)
- [+1 Array](plus-1-array)
- [Points in the circle](points-in-the-circle)
- [Polybius square cipher - encode](polybius-square-cipher-encode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# [Playing With Toy Blocks ~ Can you build a 4x4 square?](https://www.codewars.com/kata/playing-with-toy-blocks-can-you-build-a-4x4-square "https://www.codewars.com/kata/5cab471da732b30018968071")

## 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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Kata {
static boolean buildSquare(int[] blocks) {
int[] count = new int[4];
for (int b : blocks) {
count[b - 1]++;
}
return count[3] + Math.min(count[0], count[2]) + (Math.max(count[0] - count[2], 0) / 2 + count[1]) / 2 > 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class KataTests {
@Test
void sample() {
assertTrue(Kata.buildSquare(new int[]{4, 3, 2, 1, 3, 1, 1, 2, 3, 1, 1}));
assertTrue(Kata.buildSquare(new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
assertFalse(Kata.buildSquare(new int[]{3, 1, 3, 1, 3, 1, 3, 2}));
assertFalse(Kata.buildSquare(new int[]{3, 2, 3, 3, 3, 3, 3, 3, 4, 2, 4}));
assertFalse(Kata.buildSquare(new int[]{1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1}));
}
}