-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay16.java
107 lines (92 loc) · 3.34 KB
/
Day16.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.sbaars.adventofcode.year15.days;
import com.sbaars.adventofcode.year15.Day2015;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Day16 extends Day2015 {
private static final Map<String, Integer> TARGET_VALUES = Map.of(
"children", 3,
"cats", 7,
"samoyeds", 2,
"pomeranians", 3,
"akitas", 0,
"vizslas", 0,
"goldfish", 5,
"trees", 3,
"cars", 2,
"perfumes", 1
);
private final List<AuntSue> aunts = new ArrayList<>();
public Day16() {
super(16);
parseInput();
}
public static void main(String[] args) {
Day16 day = new Day16();
day.printParts();
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 16, 1);
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 16, 2);
}
private void parseInput() {
Pattern pattern = Pattern.compile("Sue (\\d+): (.*)");
Arrays.stream(day().split("\n"))
.map(pattern::matcher)
.filter(Matcher::find)
.forEach(matcher -> {
int number = Integer.parseInt(matcher.group(1));
Map<String, Integer> properties = parseProperties(matcher.group(2));
aunts.add(new AuntSue(number, properties));
});
}
private Map<String, Integer> parseProperties(String propertiesStr) {
Map<String, Integer> properties = new HashMap<>();
Pattern propertyPattern = Pattern.compile("(\\w+): (\\d+)");
Matcher propertyMatcher = propertyPattern.matcher(propertiesStr);
while (propertyMatcher.find()) {
properties.put(propertyMatcher.group(1), Integer.parseInt(propertyMatcher.group(2)));
}
return properties;
}
@Override
public Object part1() {
return aunts.stream()
.filter(this::matchesAllProperties)
.mapToInt(aunt -> aunt.number)
.findFirst()
.orElse(0);
}
@Override
public Object part2() {
return aunts.stream()
.filter(this::matchesAllPropertiesWithRanges)
.mapToInt(aunt -> aunt.number)
.findFirst()
.orElse(0);
}
private boolean matchesAllProperties(AuntSue aunt) {
return aunt.properties.entrySet().stream()
.allMatch(entry -> TARGET_VALUES.get(entry.getKey()).equals(entry.getValue()));
}
private boolean matchesAllPropertiesWithRanges(AuntSue aunt) {
return aunt.properties.entrySet().stream()
.allMatch(entry -> {
String property = entry.getKey();
int value = entry.getValue();
int target = TARGET_VALUES.get(property);
return switch (property) {
case "cats", "trees" -> value > target;
case "pomeranians", "goldfish" -> value < target;
default -> value == target;
};
});
}
private static class AuntSue {
private final int number;
private final Map<String, Integer> properties;
public AuntSue(int number, Map<String, Integer> properties) {
this.number = number;
this.properties = properties;
}
}
}