-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC2020_06.java
79 lines (67 loc) · 2.01 KB
/
AoC2020_06.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
import static com.github.pareronia.aoc.Utils.asCharacterStream;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import java.util.List;
import java.util.Set;
import com.github.pareronia.aoc.SetUtils;
import com.github.pareronia.aocd.Aocd;
import com.github.pareronia.aocd.Puzzle;
public class AoC2020_06 extends AoCBase {
private final List<String> inputs;
private AoC2020_06(final List<String> input, final boolean debug) {
super(debug);
this.inputs = input;
}
public static AoC2020_06 create(final List<String> input) {
return new AoC2020_06(input, false);
}
public static AoC2020_06 createDebug(final List<String> input) {
return new AoC2020_06(input, true);
}
@Override
public Integer solvePart1() {
return toBlocks(this.inputs).stream()
.map(b -> String.join("", b))
.collect(toList()).stream()
.map(s -> asCharacterStream(s).collect(toSet()))
.mapToInt(Set::size)
.sum();
}
@Override
public Integer solvePart2() {
return toBlocks(this.inputs).stream()
.map(bl -> bl.stream()
.map(s -> asCharacterStream(s).collect(toSet()))
.reduce(SetUtils::intersection)
.orElseThrow())
.mapToInt(Set::size)
.sum();
}
public static void main(final String[] args) throws Exception {
assert AoC2020_06.createDebug(TEST).solvePart1() == 11;
assert AoC2020_06.createDebug(TEST).solvePart2() == 6;
final Puzzle puzzle = Aocd.puzzle(2020, 6);
final List<String> inputData = puzzle.getInputData();
puzzle.check(
() -> lap("Part 1", AoC2020_06.create(inputData)::solvePart1),
() -> lap("Part 2", AoC2020_06.create(inputData)::solvePart2)
);
}
private static final List<String> TEST = splitLines(
"abc\r\n" +
"\r\n" +
"a\r\n" +
"b\r\n" +
"c\r\n" +
"\r\n" +
"ab\r\n" +
"ac\r\n" +
"\r\n" +
"a\r\n" +
"a\r\n" +
"a\r\n" +
"a\r\n" +
"\r\n" +
"b"
);
}