-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay2.java
67 lines (54 loc) · 1.85 KB
/
Day2.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
package com.sbaars.adventofcode.year23.days;
import com.sbaars.adventofcode.year23.Day2023;
import java.util.List;
import static com.sbaars.adventofcode.util.DataMapper.readString;
public class Day2 extends Day2023 {
public Day2() {
super(2);
}
public static void main(String[] args) {
new Day2().printParts();
}
public record MapGame (int num, List<MapCube> cubes) {}
public record MapCube (String s) {}
public record Game (int num, List<Cube> cubes) {}
public record Cube (List<Draw> draws) {}
public record Draw (int amount, String color) {}
@Override
public Object part1() {
return input().stream()
.filter(g -> g.cubes.stream().noneMatch(c -> c.draws.stream().anyMatch(this::invalid)))
.mapToInt(Game::num)
.sum();
}
public boolean invalid(Draw d) {
return d.amount > max(d.color);
}
public int max(String color) {
return switch (color) {
case "red" -> 12;
case "green" -> 13;
case "blue" -> 14;
default -> throw new IllegalStateException();
};
}
@Override
public Object part2() {
return input().stream()
.mapToLong(g -> findMaxProduct(g.cubes.stream().flatMap(c -> c.draws.stream()).toList()))
.sum();
}
private List<Game> input() {
return dayStream().map(s -> readString(s, "Game %i: %l(%s)", "; ", MapGame.class, MapCube.class))
.map(c -> new Game(c.num, c.cubes.stream().map(s -> readString(s.s, "%l(%i %s)", ", ", Cube.class, Draw.class)).toList()))
.toList();
}
private long findMaxProduct(List<Draw> ds) {
return getProduct(ds, "red") *
getProduct(ds, "green") *
getProduct(ds, "blue");
}
private static long getProduct(List<Draw> ds, String color) {
return ds.stream().filter(d -> d.color.equals(color)).mapToLong(d -> d.amount).max().orElse(0);
}
}