From 212785c447ffbe4310648d93c88483aeadf7ae56 Mon Sep 17 00:00:00 2001 From: PascalWo Date: Mon, 4 Jul 2022 09:59:41 +0200 Subject: [PATCH 1/3] test for day1 part two --- .../adventofcode/days/day01/Day01Test.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java b/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java index c9c773b5..68cceda8 100644 --- a/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java +++ b/src/test/java/org/haffson/adventofcode/days/day01/Day01Test.java @@ -42,4 +42,20 @@ public void test_firstPart_returnsExpectedResult() { //assert Assert.assertEquals(expectedResult, actualResult); } + + @Test + public void test_secondPart_returnsExpectedResult() { + //arrange + Day01 day01 = new Day01(fileReaders); + when(fileReaders.getInputArray("src/main/resources/puzzle_input/day1_input.txt")) + .thenReturn(new ArrayList<>(Arrays.asList(3,3,4,-2,-4))); + + String expectedResult = "Part 2 - Frequency: " + 10; + + //act + String actualResult = day01.secondPart(); + + //assert + Assert.assertEquals(expectedResult, actualResult); + } } From 9fd7caed25648f3a78526fb6392bd3f18d396b73 Mon Sep 17 00:00:00 2001 From: PascalWo Date: Mon, 4 Jul 2022 10:00:01 +0200 Subject: [PATCH 2/3] result for day1 part 2 --- .../adventofcode/days/day01/Day01.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java index 101b15c8..fb053633 100644 --- a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java +++ b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Component; import java.util.HashMap; +import java.util.HashSet; import java.util.List; /** @@ -29,7 +30,7 @@ public class Day01 implements Days { this.fileReaders = fileReaders; this.problemStatus = new HashMap<>(); this.problemStatus.put("1", ProblemStatusEnum.SOLVED); - this.problemStatus.put("2", ProblemStatusEnum.UNSOLVED); + this.problemStatus.put("2", ProblemStatusEnum.SOLVED); } @Override @@ -51,7 +52,8 @@ public String firstPart() { @Override public String secondPart() { - return null; + String fileName = "src/main/resources/puzzle_input/day1_input.txt"; + return "Part 2 - Frequency: " + calculateFrequencyPart2(fileReaders.getInputArray(fileName)); } /** @@ -65,4 +67,22 @@ private int calculateFrequency(List myIntArrayList) { .mapToInt(Integer::intValue) .sum(); } + + private int calculateFrequencyPart2(List myIntArrayList){ + int sum = 0; + HashSet found = new HashSet<>(); + boolean foundDuplicate = false; + while (!foundDuplicate) { + for (int f : myIntArrayList) { + sum += f; + if (found.contains(sum)) { + foundDuplicate = true; + break; + } else { + found.add(sum); + } + } + } + return sum; + } } From 86e862a8fa95663358e0c0698e513c51fb8b7158 Mon Sep 17 00:00:00 2001 From: PascalWo Date: Mon, 4 Jul 2022 11:03:26 +0200 Subject: [PATCH 3/3] refactored variable names --- .../org/haffson/adventofcode/days/day01/Day01.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java index fb053633..017510a7 100644 --- a/src/main/java/org/haffson/adventofcode/days/day01/Day01.java +++ b/src/main/java/org/haffson/adventofcode/days/day01/Day01.java @@ -69,20 +69,20 @@ private int calculateFrequency(List myIntArrayList) { } private int calculateFrequencyPart2(List myIntArrayList){ - int sum = 0; - HashSet found = new HashSet<>(); + int sumOfLookedUpListEntries = 0; + HashSet previousSums = new HashSet<>(); boolean foundDuplicate = false; while (!foundDuplicate) { for (int f : myIntArrayList) { - sum += f; - if (found.contains(sum)) { + sumOfLookedUpListEntries += f; + if (previousSums.contains(sumOfLookedUpListEntries)) { foundDuplicate = true; break; } else { - found.add(sum); + previousSums.add(sumOfLookedUpListEntries); } } } - return sum; + return sumOfLookedUpListEntries; } }