Skip to content

Commit

Permalink
feat: solve day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 2, 2024
1 parent cfb5b19 commit 833f883
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/adventofcode/flashk/day02/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Day 2:
# Day 2: Red-Nosed Reports

[https://adventofcode.com/2024/day/2](https://adventofcode.com/2024/day/2)
34 changes: 34 additions & 0 deletions src/main/java/com/adventofcode/flashk/day02/RedNosedReports.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.adventofcode.flashk.day02;

import java.util.List;

public class RedNosedReports {

private List<Report> reports;

public RedNosedReports(List<String> 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;
}
}
96 changes: 96 additions & 0 deletions src/main/java/com/adventofcode/flashk/day02/Report.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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;
}
}
19 changes: 15 additions & 4 deletions src/test/java/com/adventofcode/flashk/day02/Day02Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,7 +44,9 @@ public void testSolvePart1Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);


RedNosedReports reports = new RedNosedReports(inputs);
assertEquals(2,reports.solveA());
}

@Test
Expand All @@ -57,8 +60,11 @@ public void testSolvePart1Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

RedNosedReports reports = new RedNosedReports(inputs);

assertEquals(257,reports.solveA());
}


@Test
@Order(3)
Expand All @@ -71,7 +77,9 @@ public void testSolvePart2Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

RedNosedReports reports = new RedNosedReports(inputs);

assertEquals(4, reports.solveB());
}

@Test
Expand All @@ -85,6 +93,9 @@ public void testSolvePart2Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
RedNosedReports reports = new RedNosedReports(inputs);

assertEquals(328, reports.solveB());

}

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/inputs

0 comments on commit 833f883

Please sign in to comment.