Skip to content

Commit

Permalink
update 840
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Aug 9, 2024
1 parent 9d927d7 commit 440b096
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
43 changes: 23 additions & 20 deletions src/main/java/com/fishercoder/solutions/firstthousand/_840.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,36 @@ public int numMagicSquaresInside(int[][] grid) {
for (int j = 0; j < n - 2; j++) {
Set<Integer> set = new HashSet<>();
int sum = grid[i][j] + grid[i][j + 1] + grid[i][j + 2];
if (sum == grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2]
&& sum == grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2]

&& sum == grid[i][j] + grid[i + 1][j] + grid[i + 2][j]
&& sum == grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1]
&& sum == grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2]

&& sum == grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2]
&& sum == grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j]

&& set.add(grid[i][j]) && isLegit(grid[i][j])
&& set.add(grid[i][j + 1]) && isLegit(grid[i][j + 1])
&& set.add(grid[i][j + 2]) && isLegit(grid[i][j + 2])
&& set.add(grid[i + 1][j]) && isLegit(grid[i + 1][j])
&& set.add(grid[i + 1][j + 1]) && isLegit(grid[i + 1][j + 1])
&& set.add(grid[i + 1][j + 2]) && isLegit(grid[i + 1][j + 2])
&& set.add(grid[i + 2][j]) && isLegit(grid[i + 2][j])
&& set.add(grid[i + 2][j + 1]) && isLegit(grid[i + 2][j + 1])
&& set.add(grid[i + 2][j + 2]) && isLegit(grid[i + 2][j + 2])
) {
if (isValid(grid, i, j, set, sum)) {
count++;
}
}
}
return count;
}

private boolean isValid(int[][] grid, int i, int j, Set<Integer> set, int sum) {
return sum == grid[i + 1][j] + grid[i + 1][j + 1] + grid[i + 1][j + 2]
&& sum == grid[i + 2][j] + grid[i + 2][j + 1] + grid[i + 2][j + 2]

&& sum == grid[i][j] + grid[i + 1][j] + grid[i + 2][j]
&& sum == grid[i][j + 1] + grid[i + 1][j + 1] + grid[i + 2][j + 1]
&& sum == grid[i][j + 2] + grid[i + 1][j + 2] + grid[i + 2][j + 2]

&& sum == grid[i][j] + grid[i + 1][j + 1] + grid[i + 2][j + 2]
&& sum == grid[i][j + 2] + grid[i + 1][j + 1] + grid[i + 2][j]

&& set.add(grid[i][j]) && isLegit(grid[i][j])
&& set.add(grid[i][j + 1]) && isLegit(grid[i][j + 1])
&& set.add(grid[i][j + 2]) && isLegit(grid[i][j + 2])
&& set.add(grid[i + 1][j]) && isLegit(grid[i + 1][j])
&& set.add(grid[i + 1][j + 1]) && isLegit(grid[i + 1][j + 1])
&& set.add(grid[i + 1][j + 2]) && isLegit(grid[i + 1][j + 2])
&& set.add(grid[i + 2][j]) && isLegit(grid[i + 2][j])
&& set.add(grid[i + 2][j + 1]) && isLegit(grid[i + 2][j + 1])
&& set.add(grid[i + 2][j + 2]) && isLegit(grid[i + 2][j + 2]);
}

private boolean isLegit(int num) {
return num <= 9 && num >= 1;
}
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/com/fishercoder/firstthousand/_840Test.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
package com.fishercoder.firstthousand;

import com.fishercoder.solutions.firstthousand._840;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class _840Test {
private static _840.Solution1 test;
private static int[][] grid;

@BeforeClass
public static void setUp() {
@BeforeEach
public void setUp() {
test = new _840.Solution1();
}

@Test
public void test1() {
grid = new int[][]{
{4,3,8,4},
{9,5,1,9},
{2,7,6,2}
{4, 3, 8, 4},
{9, 5, 1, 9},
{2, 7, 6, 2}
};
assertEquals(1, test.numMagicSquaresInside(grid));
}

@Test
public void test2() {
grid = new int[][]{
{5,5,5},
{5,5,5},
{5,5,5}
{5, 5, 5},
{5, 5, 5},
{5, 5, 5}
};
assertEquals(0, test.numMagicSquaresInside(grid));
}

@Test
public void test3() {
grid = new int[][]{
{10,3,5},
{1,6,11},
{7,9,2}
{10, 3, 5},
{1, 6, 11},
{7, 9, 2}
};
assertEquals(0, test.numMagicSquaresInside(grid));
}
Expand Down

0 comments on commit 440b096

Please sign in to comment.