-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDay5.java
51 lines (43 loc) · 1.28 KB
/
Day5.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
package com.sbaars.adventofcode.year20.days;
import com.sbaars.adventofcode.year20.Day2020;
import java.util.HashSet;
import java.util.Set;
public class Day5 extends Day2020 {
public Day5() {
super(5);
}
public static void main(String[] args) {
new Day5().printParts();
}
@Override
public Object part1() {
return getSeatIds(dayStrings()).stream().mapToInt(e -> e).max().getAsInt();
}
private Set<Integer> getSeatIds(String[] boardingPasses) {
Set<Integer> l = new HashSet<>();
for (String s : boardingPasses) {
int rowLow = 0;
int rowHigh = 127;
int columnLow = 0;
int columnHigh = 7;
for (char c : s.toCharArray()) {
if (c == 'F') {
rowHigh -= (rowHigh - rowLow + 1) / 2;
} else if (c == 'B') {
rowLow += (rowHigh - rowLow + 1) / 2;
} else if (c == 'L') {
columnHigh -= (columnHigh - columnLow + 1) / 2;
} else if (c == 'R') {
columnLow += (columnHigh - columnLow + 1) / 2;
}
}
l.add(rowHigh * 8 + columnHigh);
}
return l;
}
@Override
public Object part2() {
Set<Integer> l = getSeatIds(dayStrings());
return l.stream().mapToInt(e -> e).filter(n -> l.contains(n) && l.contains(n + 2) && !l.contains(n + 1)).sum() + 1;
}
}