generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/main/java/com/adventofcode/flashk/day02/RedNosedReports.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule inputs
updated
from 4b6acb to 5449c5