Skip to content

Commit ab939c8

Browse files
committed
Solve day 14 2015 part 2: Reindeer Olympics
1 parent e13d3c9 commit ab939c8

File tree

1 file changed

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

1 file changed

+22
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static void main(String[] args) {
2020
Day14 day = new Day14();
2121
day.printParts();
2222
new com.sbaars.adventofcode.network.Submit().submit(day.part1(), 2015, 14, 1);
23+
new com.sbaars.adventofcode.network.Submit().submit(day.part2(), 2015, 14, 2);
2324
}
2425

2526
private void parseInput() {
@@ -46,7 +47,27 @@ public Object part1() {
4647

4748
@Override
4849
public Object part2() {
49-
return 0; // Implement in next part
50+
Map<Reindeer, Integer> points = reindeers.stream()
51+
.collect(Collectors.toMap(r -> r, r -> 0));
52+
53+
// For each second
54+
for (int second = 1; second <= RACE_DURATION; second++) {
55+
final int currentSecond = second;
56+
int maxDistance = reindeers.stream()
57+
.mapToInt(r -> r.getDistanceAfter(currentSecond))
58+
.max()
59+
.orElse(0);
60+
61+
// Award points to all reindeers in the lead
62+
reindeers.stream()
63+
.filter(r -> r.getDistanceAfter(currentSecond) == maxDistance)
64+
.forEach(r -> points.merge(r, 1, Integer::sum));
65+
}
66+
67+
return points.values().stream()
68+
.mapToInt(Integer::intValue)
69+
.max()
70+
.orElse(0);
5071
}
5172

5273
private static class Reindeer {

0 commit comments

Comments
 (0)