From e1b47c5f93b0afd417301f15f62f124c07f4fbd5 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sun, 21 Nov 2021 08:54:17 +0100 Subject: [PATCH] day02 --- src/de/domainforge/aoc2017/Day02.java | 51 ++++++++++++++++++++++ src/resources/input02 | 16 +++++++ test/de/domainforge/aoc2017/Day02Test.java | 39 +++++++++++++++++ test/resources/input02_1_1 | 3 ++ test/resources/input02_2_1 | 3 ++ 5 files changed, 112 insertions(+) create mode 100644 src/de/domainforge/aoc2017/Day02.java create mode 100644 src/resources/input02 create mode 100644 test/de/domainforge/aoc2017/Day02Test.java create mode 100644 test/resources/input02_1_1 create mode 100644 test/resources/input02_2_1 diff --git a/src/de/domainforge/aoc2017/Day02.java b/src/de/domainforge/aoc2017/Day02.java new file mode 100644 index 0000000..c19e5a2 --- /dev/null +++ b/src/de/domainforge/aoc2017/Day02.java @@ -0,0 +1,51 @@ +package de.domainforge.aoc2017; + +import de.domainforge.tools.AOCDay; +import java.util.ArrayList; + +public class Day02 extends AOCDay { + public Day02(String input) { super(input); } + + @Override + public String part1() { + String[] input = this.getInputAsArray(); + int res = 0; + for (String line : input) { + int min = Integer.MAX_VALUE; + int max = 0; + String[] items = line.strip().split("[\s\t]+"); + for (String item : items) { + int value = Integer.parseInt(item); + if (value < min) { + min = value; + } + if (value > max) { + max = value; + } + } + res += (max - min); + } + + return String.valueOf(res); + } + + @Override + public String part2() { + String[] input = this.getInputAsArray(); + int res = 0; + for (String line : input) { + ArrayList items = new ArrayList<>(); + for (String number : line.strip().split("[\s\t]+")) { + items.add(Integer.parseInt(number)); + } + for (Integer i : items) { + for (Integer j : items) { + if (i % j == 0 && i / j != 1) { + res += i / j; + } + } + } + } + return String.valueOf(res); + } +} diff --git a/src/resources/input02 b/src/resources/input02 new file mode 100644 index 0000000..19b0021 --- /dev/null +++ b/src/resources/input02 @@ -0,0 +1,16 @@ +62 1649 1731 76 51 1295 349 719 52 1984 2015 2171 981 1809 181 1715 +161 99 1506 1658 84 78 533 242 1685 86 107 1548 670 960 1641 610 +95 2420 2404 2293 542 2107 2198 121 109 209 2759 1373 1446 905 1837 111 +552 186 751 527 696 164 114 530 558 307 252 200 481 142 205 479 +581 1344 994 1413 120 112 656 1315 1249 193 1411 1280 110 103 74 1007 +2536 5252 159 179 4701 1264 1400 2313 4237 161 142 4336 1061 3987 2268 4669 +3270 1026 381 185 293 3520 1705 1610 3302 628 3420 524 3172 244 295 39 +4142 1835 4137 3821 3730 2094 468 141 150 3982 147 4271 1741 2039 4410 179 +1796 83 2039 1252 84 1641 2165 1218 1936 335 1807 2268 66 102 1977 2445 +96 65 201 275 257 282 233 60 57 200 216 134 72 105 81 212 +3218 5576 5616 5253 178 3317 6147 5973 2424 274 4878 234 200 4781 5372 276 +4171 2436 134 3705 3831 3952 2603 115 660 125 610 152 4517 587 1554 619 +2970 128 2877 1565 1001 167 254 2672 59 473 2086 181 1305 162 1663 2918 +271 348 229 278 981 1785 2290 516 473 2037 737 2291 2521 1494 1121 244 +2208 2236 1451 621 1937 1952 865 61 1934 49 1510 50 1767 59 194 1344 +94 2312 2397 333 1192 106 2713 2351 2650 2663 703 157 89 510 1824 125 \ No newline at end of file diff --git a/test/de/domainforge/aoc2017/Day02Test.java b/test/de/domainforge/aoc2017/Day02Test.java new file mode 100644 index 0000000..bc5e89d --- /dev/null +++ b/test/de/domainforge/aoc2017/Day02Test.java @@ -0,0 +1,39 @@ +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.HashMap; +import java.util.Objects; + +import static org.junit.jupiter.api.Assertions.*; + +class Day02Test { + + @Test + void part1() throws IOException { + HashMap tests = new HashMap<>(); + tests.put("input02_1_1", "18"); + + for (String inputFileName : tests.keySet()) { + File inputFile = new File(Objects.requireNonNull(Main.class.getClassLoader().getResource(inputFileName)).getPath()); + AOCDay day = new Day02(FileHelper.readFileToString(inputFile)); + assertEquals(tests.get(inputFileName), day.part1()); + } + } + + @Test + void part2() throws IOException { + HashMap tests = new HashMap<>(); + tests.put("input02_2_1", "9"); + + for (String inputFileName : tests.keySet()) { + File inputFile = new File(Objects.requireNonNull(Main.class.getClassLoader().getResource(inputFileName)).getPath()); + AOCDay day = new Day02(FileHelper.readFileToString(inputFile)); + assertEquals(tests.get(inputFileName), day.part2()); + } + } +} \ No newline at end of file diff --git a/test/resources/input02_1_1 b/test/resources/input02_1_1 new file mode 100644 index 0000000..ef93227 --- /dev/null +++ b/test/resources/input02_1_1 @@ -0,0 +1,3 @@ +5 1 9 5 +7 5 3 +2 4 6 8 \ No newline at end of file diff --git a/test/resources/input02_2_1 b/test/resources/input02_2_1 new file mode 100644 index 0000000..6cf689b --- /dev/null +++ b/test/resources/input02_2_1 @@ -0,0 +1,3 @@ +5 9 2 8 +9 4 7 3 +3 8 6 5 \ No newline at end of file