Skip to content

Commit ca23b87

Browse files
committed
Solve day 16 2015 part 2: Aunt Sue
1 parent 1009afe commit ca23b87

File tree

1 file changed

+21
-1
lines changed
  • src/main/java/com/sbaars/adventofcode/year15/days

1 file changed

+21
-1
lines changed

src/main/java/com/sbaars/adventofcode/year15/days/Day16.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static void main(String[] args) {
3232
Day16 day = new Day16();
3333
day.printParts();
3434
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 16, 1);
35+
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 16, 2);
3536
}
3637

3738
private void parseInput() {
@@ -67,14 +68,33 @@ public Object part1() {
6768

6869
@Override
6970
public Object part2() {
70-
return 0; // Implement in next part
71+
return aunts.stream()
72+
.filter(this::matchesAllPropertiesWithRanges)
73+
.mapToInt(aunt -> aunt.number)
74+
.findFirst()
75+
.orElse(0);
7176
}
7277

7378
private boolean matchesAllProperties(AuntSue aunt) {
7479
return aunt.properties.entrySet().stream()
7580
.allMatch(entry -> TARGET_VALUES.get(entry.getKey()).equals(entry.getValue()));
7681
}
7782

83+
private boolean matchesAllPropertiesWithRanges(AuntSue aunt) {
84+
return aunt.properties.entrySet().stream()
85+
.allMatch(entry -> {
86+
String property = entry.getKey();
87+
int value = entry.getValue();
88+
int target = TARGET_VALUES.get(property);
89+
90+
return switch (property) {
91+
case "cats", "trees" -> value > target;
92+
case "pomeranians", "goldfish" -> value < target;
93+
default -> value == target;
94+
};
95+
});
96+
}
97+
7898
private static class AuntSue {
7999
private final int number;
80100
private final Map<String, Integer> properties;

0 commit comments

Comments
 (0)