Skip to content

Commit 31f2a1a

Browse files
committed
finish 4 kyu Create a funnel
1 parent 2a4a3d8 commit 31f2a1a

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/main/java/com/codewars/chrisgw/algorithms/kyu_4/CreateAFunnel.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public CreateAFunnel(int height) {
2424
}
2525

2626

27-
public void fill(Character... args) {
27+
public void fill(char... args) {
2828
for (char value : args) {
2929
fill(value);
3030
}
3131
}
3232

33-
public void fill(char value) {
33+
private void fill(char value) {
3434
for (int row = 0; row < height(); row++) {
3535
char[] funnelRow = funnel[row];
3636
for (int column = 0; column < funnelRow.length; column++) {
@@ -45,20 +45,24 @@ public void fill(char value) {
4545

4646
public Character drip() {
4747
char drippedValue = funnel[0][0];
48+
if (drippedValue == ' ') {
49+
return null;
50+
}
4851
fillPlace(0, 0);
4952
return drippedValue;
5053
}
5154

5255
private void fillPlace(int row, int column) {
53-
funnel[row][column] = ' ';
54-
System.out.println(this);
5556
int rowAbove = row + 1;
5657
int leftWeight = weight(rowAbove, column, column);
5758
int rightWeight = weight(rowAbove, column + 1, column + 1);
58-
if (rightWeight > 0 && leftWeight >= rightWeight) {
59+
60+
if (leftWeight == 0 && rightWeight == 0) {
61+
funnel[row][column] = ' ';
62+
} else if (leftWeight >= rightWeight) {
5963
funnel[row][column] = funnel[rowAbove][column];
6064
fillPlace(rowAbove, column);
61-
} else if (rightWeight > 0) {
65+
} else {
6266
funnel[row][column] = funnel[rowAbove][column + 1];
6367
fillPlace(rowAbove, column + 1);
6468
}
@@ -90,7 +94,8 @@ public int height() {
9094
public String toString() {
9195
StringBuilder sb = new StringBuilder();
9296
for (int row = height() - 1; row >= 0; row--) {
93-
sb.append(" ".repeat(height() - row - 1));
97+
int leftPadding = height() - row - 1;
98+
sb.append(" ".repeat(leftPadding));
9499
sb.append("\\");
95100
for (int column = 0; column < funnel[row].length; column++) {
96101
char value = funnel[row][column];

0 commit comments

Comments
 (0)