Skip to content

Commit

Permalink
refactor: minor refactor to day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 2, 2024
1 parent cfed5c0 commit f8312a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 39 deletions.
22 changes: 4 additions & 18 deletions src/main/java/com/adventofcode/flashk/day02/RedNosedReports.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,11 @@ 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 long solveA() {
return reports.stream().filter(Report::isSafe).count();
}

public int solveB() {
int result = 0;
for(Report report : reports) {
if(report.isSafeDampener()) {
result++;
}
}

return result;
public long solveB() {
return reports.stream().filter(Report::isSafeDampener).count();
}
}
31 changes: 10 additions & 21 deletions src/main/java/com/adventofcode/flashk/day02/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,20 @@ public boolean isSafe() {
boolean expectedIncreasing = isIncreasing();
boolean isSafe = true;

int i = 1;
int lastLevel = levels.get(0);
Iterator<Integer> iterator = levels.iterator();
int previousLevel = iterator.next();

while(isSafe && i < levels.size()) {
int currentLevel = levels.get(i);
int distance = currentLevel-lastLevel;
while(isSafe && iterator.hasNext()) {
int currentLevel = iterator.next();
int distance = currentLevel-previousLevel;

if(distance == 0) {
if((distance == 0) ||
((expectedIncreasing) && (distance > 3 || distance < 0)) ||
((!expectedIncreasing) && (distance < -3 || 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++;
previousLevel = currentLevel;
}

return isSafe;
Expand Down Expand Up @@ -74,11 +67,7 @@ public boolean isSafeDampener() {

return isSafe;
}
/**
* true si creciente
* false si decreciente;
* @return
*/

private boolean isIncreasing() {
int i = 0;
boolean found = false;
Expand Down

0 comments on commit f8312a8

Please sign in to comment.