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