From 440b09618ee737a6e6338271c440554f32a2abb3 Mon Sep 17 00:00:00 2001 From: Fisher Coder Date: Fri, 9 Aug 2024 07:40:12 -0700 Subject: [PATCH] update 840 --- .../solutions/firstthousand/_840.java | 43 ++++++++++--------- .../fishercoder/firstthousand/_840Test.java | 28 ++++++------ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/fishercoder/solutions/firstthousand/_840.java b/src/main/java/com/fishercoder/solutions/firstthousand/_840.java index e281a3199a..33fe32027d 100644 --- a/src/main/java/com/fishercoder/solutions/firstthousand/_840.java +++ b/src/main/java/com/fishercoder/solutions/firstthousand/_840.java @@ -13,26 +13,7 @@ public int numMagicSquaresInside(int[][] grid) { for (int j = 0; j < n - 2; j++) { Set 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++; } } @@ -40,6 +21,28 @@ public int numMagicSquaresInside(int[][] grid) { return count; } + private boolean isValid(int[][] grid, int i, int j, Set 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; } diff --git a/src/test/java/com/fishercoder/firstthousand/_840Test.java b/src/test/java/com/fishercoder/firstthousand/_840Test.java index 709fea4daf..ebecafad46 100644 --- a/src/test/java/com/fishercoder/firstthousand/_840Test.java +++ b/src/test/java/com/fishercoder/firstthousand/_840Test.java @@ -1,26 +1,26 @@ 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)); } @@ -28,9 +28,9 @@ public void test1() { @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)); } @@ -38,9 +38,9 @@ public void test2() { @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)); }