Skip to content

Commit 857ddbe

Browse files
* docs: kata description * docs: sync progress * feat: kata/playing-with-toy-blocks-can-you-build-a-4x4-square --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent cf09900 commit 857ddbe

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ slug.
2525

2626
| [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) |
2727
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
28-
| 0 | 1 | 2 | 26 | 48 | 443 | 605 | 228 | 56 | 82 |
28+
| 0 | 1 | 2 | 26 | 48 | 444 | 605 | 228 | 56 | 82 |
2929

3030
**Note:** The source code is written in Java 17 and may use language features that are incompatible
3131
with Java 8, 11.

kata/6-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
- [Playing on a chessboard](playing-on-a-chessboard)
283283
- [Playing with digits](playing-with-digits)
284284
- [Playing with passphrases](playing-with-passphrases)
285+
- [Playing With Toy Blocks ~ Can you build a 4x4 square?](playing-with-toy-blocks-can-you-build-a-4x4-square)
285286
- [+1 Array](plus-1-array)
286287
- [Points in the circle](points-in-the-circle)
287288
- [Polybius square cipher - encode](polybius-square-cipher-encode)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [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")
2+
3+
## An Invitation
4+
5+
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
6+
3-year-old exclaims, "Look, I made a square!", then pointing to a pile of blocks, "Can _you_ do it?"
7+
8+
## Our Blocks
9+
10+
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
11+
different sizes of block: `1x1`, `1x2`, `1x3`, and `1x4`. The smallest one represents the area of a square, the other three are rectangular,
12+
and all differ by their width. Integers matching these four widths are used to represent the blocks in the input.
13+
14+
## The Square
15+
16+
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
17+
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
18+
are five types of row you could build:
19+
20+
1) 1 four-unit block
21+
2) 1 three-unit block plus 1 one-unit bock (in either order)
22+
3) 2 two-unit blocks
23+
4) 1 two-unit block plus 2 one-unit blocks (in any order)
24+
5) 4 one-unit blocks
25+
26+
## Some Notes
27+
28+
* Amounts for all four block sizes will each vary from `0` to `16`.
29+
* The total size of the pile will also vary from `0` to `16`.
30+
* A valid square doesn't have to use all given blocks.
31+
* The order of rows is irrelevant.
32+
33+
## Some Examples
34+
35+
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
36+
their four rows:
37+
38+
ONE
39+
40+
1. 1 four-unit block
41+
2. 2 two-unit blocks
42+
3. 1 four-unit block
43+
4. 4 one-unit blocks
44+
45+
TWO
46+
47+
1. 1 three-unit block plus 1 one-unit block
48+
2. 2 two-unit blocks
49+
3. 1 four-unit block
50+
4. 1 one-unit block plus 1 three-unit block
51+
52+
THREE
53+
54+
1. 2 two-unit blocks
55+
2. 1 three-unit block plus 1 one-unit block
56+
3. 1 four-unit block
57+
4. 2 one-unit blocks plus 1 two-unit block
58+
59+
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
60+
be impressed.
61+
62+
NONE
63+
64+
1. 1 four-unit block
65+
2. 2 two-unit blocks
66+
3. 1 three-unit block plus 1 one-unit block
67+
4. (here only sadness)
68+
69+
## Input
70+
71+
```
72+
blocks ~ array of random integers (1 <= x <= 4)
73+
```
74+
75+
## Output
76+
77+
```
78+
true or false ~ whether you can build a square
79+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
interface Kata {
2+
static boolean buildSquare(int[] blocks) {
3+
int[] count = new int[4];
4+
for (int b : blocks) {
5+
count[b - 1]++;
6+
}
7+
return count[3] + Math.min(count[0], count[2]) + (Math.max(count[0] - count[2], 0) / 2 + count[1]) / 2 > 3;
8+
}
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import static org.junit.jupiter.api.Assertions.assertFalse;
2+
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
class KataTests {
7+
@Test
8+
void sample() {
9+
assertTrue(Kata.buildSquare(new int[]{4, 3, 2, 1, 3, 1, 1, 2, 3, 1, 1}));
10+
assertTrue(Kata.buildSquare(new int[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
11+
assertFalse(Kata.buildSquare(new int[]{3, 1, 3, 1, 3, 1, 3, 2}));
12+
assertFalse(Kata.buildSquare(new int[]{3, 2, 3, 3, 3, 3, 3, 3, 4, 2, 4}));
13+
assertFalse(Kata.buildSquare(new int[]{1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1}));
14+
}
15+
}

0 commit comments

Comments
 (0)