Day 2 implementation in Java

main
MiguelMLorente 2023-12-02 14:02:31 +01:00
parent 877d1c4fa5
commit e98bb258b7
4 changed files with 118 additions and 1 deletions

View File

@ -0,0 +1,29 @@
package day2;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Getter
public class Game {
private static final String GAME_ID_PREFIX = "Game ";
private static final String GAME_ID_SEPARATOR = ":";
private static final String ROUNDS_SEPARATOR = ";";
private final Integer id;
private final List<Round> rounds;
public Game(String gameRepresentation) {
String[] splitGameRepresentation = gameRepresentation.split(GAME_ID_SEPARATOR, 2);
this.id = Integer.parseInt(splitGameRepresentation[0].replace(GAME_ID_PREFIX, ""));
this.rounds = Arrays.stream(splitGameRepresentation[1].split(ROUNDS_SEPARATOR))
.map(Round::new)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,43 @@
package day2;
import util.FileReader;
import java.util.List;
public class Main {
private static final Round ROUND_MAXIMUM_THRESHOLD = new Round(12, 14, 13);
public static void main(String[] args) {
String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day2\\input.txt";
FileReader reader = new FileReader(inputFilePath);
List<String> inputData = reader.readContents();
Integer validGamesCount = inputData.stream()
.map(Game::new)
.filter(game -> {
Round round = game.getRounds().stream()
.reduce(Round::mergePrevailMaximum)
.orElseThrow(RuntimeException::new);
return round.getRedCount() <= ROUND_MAXIMUM_THRESHOLD.getRedCount()
&& round.getBlueCount() <= ROUND_MAXIMUM_THRESHOLD.getBlueCount()
&& round.getGreenCount() <= ROUND_MAXIMUM_THRESHOLD.getGreenCount();
}
).map(Game::getId)
.reduce(Integer::sum)
.orElse(0);
System.out.println(validGamesCount);
Integer cubesPowerCount = inputData.stream()
.map(Game::new)
.map(Game::getRounds)
.map(rounds -> rounds.stream()
.reduce(Round::mergePrevailMaximum)
.orElseThrow(RuntimeException::new))
.map(round -> round.getGreenCount() * round.getRedCount() * round.getBlueCount())
.reduce(Integer::sum)
.orElse(0);
System.out.println(cubesPowerCount);
}
}

View File

@ -0,0 +1,45 @@
package day2;
import lombok.Getter;
import java.util.Arrays;
@Getter
public class Round {
private static final String ROUND_COLORS_SEPARATOR = ",";
private static final String RED_SUFFIX = "red";
private static final String BLUE_SUFFIX = "blue";
private static final String GREED_SUFFIX = "green";
private Integer redCount = 0;
private Integer blueCount = 0;
private Integer greenCount = 0;
public Round(Integer redCount, Integer blueCount, Integer greenCount) {
this.redCount = redCount;
this.blueCount = blueCount;
this.greenCount = greenCount;
}
public Round(String roundRepresentation) {
Arrays.stream(roundRepresentation.split(ROUND_COLORS_SEPARATOR, 3))
.map(colorRepresentation -> colorRepresentation.replaceAll(" ", ""))
.forEach(colorRepresentation -> {
if (colorRepresentation.contains(RED_SUFFIX)) {
this.redCount = Integer.parseInt(colorRepresentation.replace(RED_SUFFIX, ""));
} else if (colorRepresentation.contains(BLUE_SUFFIX)) {
this.blueCount = Integer.parseInt(colorRepresentation.replace(BLUE_SUFFIX, ""));
} else if (colorRepresentation.contains(GREED_SUFFIX)) {
this.greenCount = Integer.parseInt(colorRepresentation.replace(GREED_SUFFIX, ""));
}
});
}
public Round mergePrevailMaximum(Round otherRound) {
return new Round(
Math.max(this.redCount, otherRound.redCount),
Math.max(this.blueCount, otherRound.blueCount),
Math.max(this.greenCount, otherRound.greenCount)
);
}
}

View File

@ -21,7 +21,7 @@ public class FileReader {
scanner.useDelimiter("\n"); scanner.useDelimiter("\n");
List<String> data = new ArrayList<>(); List<String> data = new ArrayList<>();
while (scanner.hasNext()) { while (scanner.hasNext()) {
data.add(scanner.next()); data.add(scanner.next().replace("\r", ""));
} }
return data; return data;
} catch (Exception e) { } catch (Exception e) {