diff --git a/src/de/domainforge/aoc2017/Day01.java b/src/de/domainforge/aoc2017/Day01.java index e51e84b..e463b8a 100644 --- a/src/de/domainforge/aoc2017/Day01.java +++ b/src/de/domainforge/aoc2017/Day01.java @@ -2,6 +2,8 @@ package de.domainforge.aoc2017; import de.domainforge.tools.AOCDay; +import java.nio.charset.StandardCharsets; + public class Day01 extends AOCDay { public Day01(String input) { super(input); @@ -9,12 +11,32 @@ public class Day01 extends AOCDay { @Override public String part1() { - int[] input = this.getInputIntArray(); - return String.valueOf(input[0]); + byte[] input = this.input.getBytes(StandardCharsets.UTF_8); + int res = 0; + if (input[0] == input[input.length - 1]) { + res += input[0] - 48; + } + for (int i = 0; i < input.length - 1; i++) { + if (input[i] == input[i+1]) { + res += input[i] - 48; + } + } + + return String.valueOf(res); } @Override public String part2() { - return "bar"; + byte[] input = this.input.getBytes(StandardCharsets.UTF_8); + int res = 0; + int half = input.length / 2; + + for (int i = 0; i < half; i++) { + if (input[i] == input[half + i]) { + res += (input[i] - 48) * 2; + } + } + + return String.valueOf(res); } } diff --git a/test/de/domainforge/aoc2017/Day01Test.java b/test/de/domainforge/aoc2017/Day01Test.java index 0fc47f9..6aa9c9d 100644 --- a/test/de/domainforge/aoc2017/Day01Test.java +++ b/test/de/domainforge/aoc2017/Day01Test.java @@ -1,16 +1,45 @@ package de.domainforge.aoc2017; +import de.domainforge.tools.AOCDay; +import de.domainforge.tools.FileHelper; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.IOException; +import java.util.*; + import static org.junit.jupiter.api.Assertions.*; class Day01Test { @Test - void part1() { + void part1() throws IOException { + HashMap tests = new HashMap<>(); + tests.put("input01_1_1", "3"); + tests.put("input01_1_2", "4"); + tests.put("input01_1_3", "0"); + tests.put("input01_1_4", "9"); + + for (String inputFileName : tests.keySet()) { + File inputFile = new File(Objects.requireNonNull(Main.class.getClassLoader().getResource(inputFileName)).getPath()); + AOCDay day01 = new Day01(FileHelper.readFileToString(inputFile)); + assertEquals(tests.get(inputFileName), day01.part1()); + } } @Test - void part2() { + void part2() throws IOException { + HashMap tests = new HashMap<>(); + tests.put("input01_2_1", "6"); + tests.put("input01_2_2", "0"); + tests.put("input01_2_3", "4"); + tests.put("input01_2_4", "12"); + tests.put("input01_2_5", "4"); + + for (String inputFileName : tests.keySet()) { + File inputFile = new File(Objects.requireNonNull(Main.class.getClassLoader().getResource(inputFileName)).getPath()); + AOCDay day01 = new Day01(FileHelper.readFileToString(inputFile)); + assertEquals(tests.get(inputFileName), day01.part2()); + } } } \ No newline at end of file diff --git a/test/resources/input01_1_1 b/test/resources/input01_1_1 index 7fdc975..4ee16cc 100644 --- a/test/resources/input01_1_1 +++ b/test/resources/input01_1_1 @@ -1,2 +1 @@ -3 1122 \ No newline at end of file diff --git a/test/resources/input01_1_2 b/test/resources/input01_1_2 index 3108d11..d1d06ad 100644 --- a/test/resources/input01_1_2 +++ b/test/resources/input01_1_2 @@ -1,2 +1 @@ -4 1111 \ No newline at end of file diff --git a/test/resources/input01_1_3 b/test/resources/input01_1_3 index 6d0f86b..274c005 100644 --- a/test/resources/input01_1_3 +++ b/test/resources/input01_1_3 @@ -1,2 +1 @@ -0 1234 \ No newline at end of file diff --git a/test/resources/input01_1_4 b/test/resources/input01_1_4 index 64f0b35..4864670 100644 --- a/test/resources/input01_1_4 +++ b/test/resources/input01_1_4 @@ -1,2 +1 @@ -9 91212129 \ No newline at end of file diff --git a/test/resources/input01_2_1 b/test/resources/input01_2_1 new file mode 100644 index 0000000..f73dd74 --- /dev/null +++ b/test/resources/input01_2_1 @@ -0,0 +1 @@ +1212 \ No newline at end of file diff --git a/test/resources/input01_2_2 b/test/resources/input01_2_2 new file mode 100644 index 0000000..35c53b6 --- /dev/null +++ b/test/resources/input01_2_2 @@ -0,0 +1 @@ +1221 \ No newline at end of file diff --git a/test/resources/input01_2_3 b/test/resources/input01_2_3 new file mode 100644 index 0000000..b12c5cc --- /dev/null +++ b/test/resources/input01_2_3 @@ -0,0 +1 @@ +123425 \ No newline at end of file diff --git a/test/resources/input01_2_4 b/test/resources/input01_2_4 new file mode 100644 index 0000000..93a5f6a --- /dev/null +++ b/test/resources/input01_2_4 @@ -0,0 +1 @@ +123123 \ No newline at end of file diff --git a/test/resources/input01_2_5 b/test/resources/input01_2_5 new file mode 100644 index 0000000..cb05196 --- /dev/null +++ b/test/resources/input01_2_5 @@ -0,0 +1 @@ +12131415 \ No newline at end of file