Skip to content
Snippets Groups Projects
Commit 15c715e4 authored by LUDMANN Pierre's avatar LUDMANN Pierre
Browse files

[day2] part1 use parse

parent be6bb389
Branches day2-ludmann1
No related tags found
No related merge requests found
......@@ -8,35 +8,29 @@ import static java.lang.Integer.parseInt;
public class Day2 extends Day {
HashMap<String, Integer> limit = new HashMap<>(3);
Map<Integer, Map<String, Integer>> maxQty = new HashMap<>(input.size());
Map<Integer, Map<String, Integer>> maxQtyByGame =
new HashMap<>(input.size());
Day2(String inputName) {
super(inputName);
parse();
limit.put("red", 12);
limit.put("green", 13);
limit.put("blue", 14);
}
String part1() {
int ans = 0;
for (String game : input) {
String[] tmp = game.split(": ", 2);
int gameId = parseInt(tmp[0].substring("Game ".length()));
boolean possible = true;
for (String set : tmp[1].split("; ")) {
for (String cubes : set.split(", ")) {
String[] colorQty = cubes.split(" ", 2);
if (parseInt(colorQty[0]) > limit.get(colorQty[1])) {
possible = false;
break;
}
}
if (!possible) break;
}
if (possible) ans += gameId;
}
HashMap<String, Integer> limits = new HashMap<>(3);
limits.put("red", 12);
limits.put("green", 13);
limits.put("blue", 14);
return part1(limits);
}
String part1(Map<String, Integer> limits) {
int ans = maxQtyByGame.keySet()
.stream()
.filter(gameId -> maxQtyByGame.get(gameId).entrySet().stream()
.allMatch(colorMax -> colorMax.getValue() <=
limits.get(colorMax.getKey())))
.mapToInt(x -> x).sum();
return String.valueOf(ans);
}
......@@ -53,12 +47,12 @@ public class Day2 extends Day {
currentMaxQty.get(colorQty[1])));
}
Integer gameId = Integer.valueOf(tmp[0].substring(5));
maxQty.put(gameId, currentMaxQty);
maxQtyByGame.put(gameId, currentMaxQty);
}
}
String part2() {
int ans = maxQty.values().stream()
int ans = maxQtyByGame.values().stream()
.map(game -> game.values().stream().mapToInt(x -> x)
.reduce(1, (x, y) -> x * y)).reduce(0, Integer::sum);
return String.valueOf(ans);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment