-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5Part1.java
60 lines (49 loc) · 1.73 KB
/
Day5Part1.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
package uk.oczadly.karl.aoc20.solution.day5;
import uk.oczadly.karl.aoc20.input.IllegalInputException;
import uk.oczadly.karl.aoc20.input.PuzzleInput;
import uk.oczadly.karl.aoc20.PuzzleSolution;
/**
* @author Karl Oczadly
*/
public class Day5Part1 extends PuzzleSolution {
public Day5Part1() { super(5, 1); } // Initializes the day and part number
@Override
public Object solve(PuzzleInput input) {
return input.asStream()
.map(Day5Part1::parseSeat)
.mapToInt(Seat::getId)
.max().getAsInt();
}
/** Parses a Seat object (row and column) from a string input */
private static Seat parseSeat(String str) {
return new Seat(
calcBinaryNumber(str.substring(0, 7)),
calcBinaryNumber(str.substring(7, 10)));
}
/** Calculates the binary number from a sequence of steps. */
private static int calcBinaryNumber(String str) {
int lower = 0, upper = (1 << str.length()) - 1;
for (char c : str.toCharArray()) {
int range = upper - lower + 1;
switch (c) {
case 'F': case 'L':
upper -= range / 2; break;
case 'B': case 'R':
lower += range / 2; break;
default: throw new IllegalInputException("Unknown movement char.");
}
}
if (lower != upper) throw new AssertionError();
return lower;
}
static class Seat {
final int row, column;
public Seat(int row, int column) {
this.row = row;
this.column = column;
}
public int getId() {
return row * 8 + column;
}
}
}