From f55dcf62b8ec26ccc0c885ae6a5171b872dbc665 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Fri, 9 Aug 2024 21:20:54 -0500 Subject: [PATCH] Fix Utilities.getAnswer() --- .gitignore | 1 + README.rst | 2 +- docs/index.rst | 2 +- java/src/main/java/euler/lib/Utilities.java | 21 +++++---------------- java/src/main/java/euler/p0022.java | 2 +- java/src/test/java/EulerTest.java | 19 ++++++++++--------- 6 files changed, 19 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 4f8e2fb6..74ab857f 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ rust/target **/dist/* docs/_static/test-js.html javascript/src/lib/fallbacks +java/target \ No newline at end of file diff --git a/README.rst b/README.rst index 080858bb..551389c2 100644 --- a/README.rst +++ b/README.rst @@ -73,7 +73,7 @@ Olivia's Project Euler Solutions | | | | |CodeQL| |br| | | | | | |C#-lint| | +------------+--------------------------+--------+-------------------+ -| Java | Java 8+ | 6 | |Javai| |br| | +| Java | Java 8+ | 7 | |Javai| |br| | | | | | |Ja-Cov| |br| | | | | | |CodeQL| |br| | | | | | |Java-lint| | diff --git a/docs/index.rst b/docs/index.rst index a477acb0..f87273d8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -111,7 +111,7 @@ Problems Solved +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`21` | | | | | |:py-d:`0021`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ -|:prob:`22` |:c-d:`0022` |:cp-d:`0022`|:cs-d:`0022`|:ja-i:`0022`|:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`| +|:prob:`22` |:c-d:`0022` |:cp-d:`0022`|:cs-d:`0022`|:ja-d:`0022`|:js-d:`0022`|:py-d:`0022`|:rs-d:`0022`| +-----------+------------+------------+------------+------------+------------+------------+------------+ |:prob:`23` | | | | | |:py-d:`0023`| | +-----------+------------+------------+------------+------------+------------+------------+------------+ diff --git a/java/src/main/java/euler/lib/Utilities.java b/java/src/main/java/euler/lib/Utilities.java index a1620ca0..221b05d2 100644 --- a/java/src/main/java/euler/lib/Utilities.java +++ b/java/src/main/java/euler/lib/Utilities.java @@ -1,10 +1,8 @@ -package euler; +package euler.lib; import java.io.BufferedReader; -import java.io.File; import java.io.IOException; import java.io.StringReader; -import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -12,19 +10,9 @@ public class Utilities { private static Path getDataPath(String name) throws IOException { - String absolutePath; - try { - ClassLoader classLoader = Utilities.class.getClassLoader(); - File file = new File(classLoader.getResource("Utilities.class").toURI()); - absolutePath = file.getAbsolutePath(); - } catch (URISyntaxException e) { - e.printStackTrace(); - throw new IOException(); - } - if (absolutePath == null) { - throw new IOException("Unable to determine file path."); - } - Path filePath = Paths.get(absolutePath).getParent().resolve("../../../../../_data").resolve(name); + Path classPath = Paths.get(Utilities.class.getProtectionDomain().getCodeSource().getLocation().getPath()); + Path classDir = classPath.getParent(); + Path filePath = classDir.getParent().getParent().resolve("_data").resolve(name); return filePath.toAbsolutePath(); } @@ -41,6 +29,7 @@ public static Object getAnswer(long n) throws IOException { String csvContent = getDataFileText("answers.csv"); try (BufferedReader reader = new BufferedReader(new StringReader(csvContent))) { String line; + reader.readLine(); while ((line = reader.readLine()) != null) { String[] arr = line.split("\t"); if (arr.length < 4) continue; diff --git a/java/src/main/java/euler/p0022.java b/java/src/main/java/euler/p0022.java index b8510359..d397a96f 100644 --- a/java/src/main/java/euler/p0022.java +++ b/java/src/main/java/euler/p0022.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.util.Arrays; -import euler.Utilities; +import euler.lib.Utilities; public class p0022 implements IEuler { @Override diff --git a/java/src/test/java/EulerTest.java b/java/src/test/java/EulerTest.java index ce4e5b2a..6627e3b3 100644 --- a/java/src/test/java/EulerTest.java +++ b/java/src/test/java/EulerTest.java @@ -1,5 +1,3 @@ -package euler; - import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.api.Assertions; @@ -8,6 +6,9 @@ import java.lang.reflect.Method; import java.util.stream.Stream; +import euler.*; +import euler.lib.Utilities; + public class EulerTest { private static final long ONE_MINUTE_NS = 60_000_000_000L; // 1 minute in nanoseconds @@ -15,12 +16,12 @@ public class EulerTest { static Stream data() throws IOException { return Stream.of( new Object[]{p0000.class, false, 0}, - new Object[]{p0001.class, false, 233168}, - new Object[]{p0002.class, false, 4613732}, - new Object[]{p0004.class, false, 906609}, - new Object[]{p0006.class, false, 25164150}, + new Object[]{p0001.class, false, Utilities.getAnswer(1)}, + new Object[]{p0002.class, false, Utilities.getAnswer(2)}, + new Object[]{p0004.class, false, Utilities.getAnswer(4)}, + new Object[]{p0006.class, false, Utilities.getAnswer(6)}, // new Object[]{p0008.class, false, Utilities.getAnswer(8)}, - new Object[]{p0009.class, false, 31875000}, + new Object[]{p0009.class, false, Utilities.getAnswer(9)}, // new Object[]{p0011.class, false, Utilities.getAnswer(11)}, // new Object[]{p0013.class, false, Utilities.getAnswer(13)}, // new Object[]{p0014.class, false, Utilities.getAnswer(14)}, @@ -28,10 +29,10 @@ static Stream data() throws IOException { // new Object[]{p0016.class, false, Utilities.getAnswer(16)}, // new Object[]{p0017.class, false, Utilities.getAnswer(17)}, // new Object[]{p0020.class, false, Utilities.getAnswer(20)}, - // new Object[]{p0022.class, false, 871198282}, + new Object[]{p0022.class, false, Utilities.getAnswer(22)}, // new Object[]{p0034.class, false, Utilities.getAnswer(34)}, // new Object[]{p0076.class, true, Utilities.getAnswer(76)}, - new Object[]{p0836.class, false, "aprilfoolsjoke"} + new Object[]{p0836.class, false, Utilities.getAnswer(836)} ); }