day01 complete including tests; need better Main.main() ... but works for now

This commit is contained in:
Stefan Harmuth 2021-11-21 07:40:46 +01:00
parent 42d678b8ca
commit d8dcf50620
11 changed files with 61 additions and 9 deletions

View File

@ -2,6 +2,8 @@ package de.domainforge.aoc2017;
import de.domainforge.tools.AOCDay; import de.domainforge.tools.AOCDay;
import java.nio.charset.StandardCharsets;
public class Day01 extends AOCDay { public class Day01 extends AOCDay {
public Day01(String input) { public Day01(String input) {
super(input); super(input);
@ -9,12 +11,32 @@ public class Day01 extends AOCDay {
@Override @Override
public String part1() { public String part1() {
int[] input = this.getInputIntArray(); byte[] input = this.input.getBytes(StandardCharsets.UTF_8);
return String.valueOf(input[0]); 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 @Override
public String part2() { 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);
} }
} }

View File

@ -1,16 +1,45 @@
package de.domainforge.aoc2017; package de.domainforge.aoc2017;
import de.domainforge.tools.AOCDay;
import de.domainforge.tools.FileHelper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class Day01Test { class Day01Test {
@Test @Test
void part1() { void part1() throws IOException {
HashMap<String, String> 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 @Test
void part2() { void part2() throws IOException {
HashMap<String, String> 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());
}
} }
} }

View File

@ -1,2 +1 @@
3
1122 1122

View File

@ -1,2 +1 @@
4
1111 1111

View File

@ -1,2 +1 @@
0
1234 1234

View File

@ -1,2 +1 @@
9
91212129 91212129

View File

@ -0,0 +1 @@
1212

View File

@ -0,0 +1 @@
1221

View File

@ -0,0 +1 @@
123425

View File

@ -0,0 +1 @@
123123

View File

@ -0,0 +1 @@
12131415