Skip to content

Commit

Permalink
day 2 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
leibi committed Dec 7, 2023
1 parent 525faf1 commit 8a5acec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/java/net/leibi/adventofcode2023/day2/Day2.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ public int getSumOfPossibleGames(String input) {
return listOfGames.stream().filter(this::gameIsPossible).mapToInt(Game::id).sum();
}

public Integer getPowerSum(String input) {
return getListOfGames(input).stream()
.map(this::getSmallestCubeNumber)
.mapToInt(rgb -> rgb.red * rgb.blue * rgb.green)
.sum();
}

private RGB getSmallestCubeNumber(Game game) {
var green = game.reveal.stream().mapToInt(rgb -> rgb.green).filter(x -> x>0).max().orElse(0);
var red = game.reveal.stream().mapToInt(rgb -> rgb.red).filter(x -> x>0).max().orElse(0);
var blue = game.reveal.stream().mapToInt(rgb -> rgb.blue).filter(x -> x>0).max().orElse(0);
log.info("{} -> {},{},{}", game, red, green, blue);
return new RGB(red, green, blue);
}

private boolean gameIsPossible(Game game) {
return game.reveal.stream()
.allMatch(g -> g.blue <= cntBlue && g.red <= cntRed && g.green <= cntGreen);
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/net/leibi/adventofcode2023/day2/Day2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ void testDay2Small() {
assertThat(day2.getSumOfPossibleGames(Input.SMALL)).isEqualTo(8);
}

@Test
void testDay2_2Small() {
assertThat(day2.getPowerSum(Input.SMALL)).isEqualTo(2286);
}

@Test
void testDay2_2Big() {
assertThat(day2.getPowerSum(Input.BIG)).isEqualTo(2286);
}

@Test
void testDay2Big() {
assertThat(day2.getSumOfPossibleGames(Input.BIG)).isEqualTo(2256);
assertThat(day2.getSumOfPossibleGames(Input.BIG)).isEqualTo(74229);
}

}

0 comments on commit 8a5acec

Please sign in to comment.