diff --git a/src/main/java/com/adventofcode/flashk/day02/README.md b/src/main/java/com/adventofcode/flashk/day02/README.md index 77c336a..c59ea48 100644 --- a/src/main/java/com/adventofcode/flashk/day02/README.md +++ b/src/main/java/com/adventofcode/flashk/day02/README.md @@ -1,3 +1,3 @@ -# Day 2: +# Day 2: Red-Nosed Reports [https://adventofcode.com/2024/day/2](https://adventofcode.com/2024/day/2) diff --git a/src/main/java/com/adventofcode/flashk/day02/RedNosedReports.java b/src/main/java/com/adventofcode/flashk/day02/RedNosedReports.java new file mode 100644 index 0000000..a38293b --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day02/RedNosedReports.java @@ -0,0 +1,34 @@ +package com.adventofcode.flashk.day02; + +import java.util.List; + +public class RedNosedReports { + + private List reports; + + public RedNosedReports(List inputs) { + reports = inputs.stream().map(Report::new).toList(); + } + + public int solveA() { + int result = 0; + for(Report report : reports) { + if(report.isSafe()) { + result++; + } + } + + return result; + } + + public int solveB() { + int result = 0; + for(Report report : reports) { + if(report.isSafeDampener()) { + result++; + } + } + + return result; + } +} diff --git a/src/main/java/com/adventofcode/flashk/day02/Report.java b/src/main/java/com/adventofcode/flashk/day02/Report.java new file mode 100644 index 0000000..b62ec49 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day02/Report.java @@ -0,0 +1,96 @@ +package com.adventofcode.flashk.day02; + +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; + +@Getter +@Setter +public class Report { + + private List levels; + + public Report(String input) { + // Warning: do not use .toList() as it would create an immutable list + levels = Arrays.stream(input.split(StringUtils.SPACE)).map(Integer::valueOf).collect(Collectors.toList()); + } + + public boolean isSafe() { + boolean expectedIncreasing = isIncreasing(); + boolean isSafe = true; + + int i = 1; + int lastLevel = levels.get(0); + + while(isSafe && i < levels.size()) { + int currentLevel = levels.get(i); + int distance = currentLevel-lastLevel; + + if(distance == 0) { + isSafe = false; + } else if(expectedIncreasing) { + if(distance > 3 || distance < 0) { + isSafe = false; + } + } else { + if(distance < -3 || distance > 0) { + isSafe = false; + } + } + + lastLevel = currentLevel; + i++; + } + + return isSafe; + } + + public boolean isSafeDampener() { + if(isSafe()) { + return true; + } + + List copyLevels = new ArrayList<>(levels); + + boolean isSafe = false; + int i = 0; + + while(!isSafe && i < levels.size()) { + + levels.remove(i); + isSafe = isSafe(); + + if(!isSafe()) { + levels = new ArrayList<>(copyLevels); + i++; + } + } + + return isSafe; + } + /** + * true si creciente + * false si decreciente; + * @return + */ + private boolean isIncreasing() { + int i = 0; + boolean found = false; + int distance = 0; + while(!found && i < levels.size()-1) { + distance = levels.get(i+1)-levels.get(i); + if(distance != 0) { + found = true; + } + i++; + } + + return distance > 0; + } +} diff --git a/src/test/java/com/adventofcode/flashk/day02/Day02Test.java b/src/test/java/com/adventofcode/flashk/day02/Day02Test.java index 11308f5..18562d4 100644 --- a/src/test/java/com/adventofcode/flashk/day02/Day02Test.java +++ b/src/test/java/com/adventofcode/flashk/day02/Day02Test.java @@ -19,9 +19,10 @@ import com.adventofcode.flashk.common.test.utils.Timer; import com.adventofcode.flashk.common.test.utils.Input; +import static org.junit.jupiter.api.Assertions.assertEquals; + @DisplayName(TestDisplayName.DAY_02) @TestMethodOrder(OrderAnnotation.class) -@Disabled // TODO Remove comment when implemented public class Day02Test extends PuzzleTest { private final static String INPUT_FOLDER = TestFolder.DAY_02; @@ -43,7 +44,9 @@ public void testSolvePart1Sample() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - + + RedNosedReports reports = new RedNosedReports(inputs); + assertEquals(2,reports.solveA()); } @Test @@ -57,8 +60,11 @@ public void testSolvePart1Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - + RedNosedReports reports = new RedNosedReports(inputs); + + assertEquals(257,reports.solveA()); } + @Test @Order(3) @@ -71,7 +77,9 @@ public void testSolvePart2Sample() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - + RedNosedReports reports = new RedNosedReports(inputs); + + assertEquals(4, reports.solveB()); } @Test @@ -85,6 +93,9 @@ public void testSolvePart2Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); + RedNosedReports reports = new RedNosedReports(inputs); + + assertEquals(328, reports.solveB()); } diff --git a/src/test/resources/inputs b/src/test/resources/inputs index 4b6acbf..5449c50 160000 --- a/src/test/resources/inputs +++ b/src/test/resources/inputs @@ -1 +1 @@ -Subproject commit 4b6acbf2976dcb15bf592790fb857f0177ba056f +Subproject commit 5449c504153b1c57ca7aca20bdce5e8894bd1d0e