-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.java
More file actions
57 lines (49 loc) · 1.87 KB
/
Day03.java
File metadata and controls
57 lines (49 loc) · 1.87 KB
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
import java.util.List;
public class Day03 {
public static void Run(List<String> input) {
//part1(input);
part2(input);
}
public static void part1(List<String> input)
{
Long totalJoltage = 0L;
for(String line:input){
Long bankJoltage = MaxBankJoltage(line, 2);
totalJoltage += bankJoltage;
}
System.out.println("03.1: "+Long.toUnsignedString(totalJoltage));
}
public static void part2(List<String> input)
{
Long totalJoltage = 0L;
for(String line:input){
Long bankJoltage = MaxBankJoltage(line, 12);
totalJoltage += bankJoltage;
}
System.out.println("03.2: "+Long.toUnsignedString(totalJoltage));
}
public static Long MaxBankJoltage(String batteryJoltages, int maxDigits){
if (maxDigits >= batteryJoltages.length()){
return Long.parseLong(batteryJoltages);
}
String bankJoltage="";
int previousDigitIndex=-1;// index where previous digit was selected
// digitPlace is the place value of the next digit to select (0-based index from right)
for(int digitPlace = maxDigits-1; digitPlace >= 0; digitPlace--){
int digitIndex = previousDigitIndex+1;
char nextDigit=batteryJoltages.charAt(digitIndex);
for (int i=previousDigitIndex+1; i<batteryJoltages.length()-digitPlace; i++){
if (nextDigit=='9'){
break; // can't do better than 9
}
if (batteryJoltages.charAt(i)>nextDigit){
nextDigit=batteryJoltages.charAt(i);
digitIndex=i;
}
}
previousDigitIndex=digitIndex;
bankJoltage += nextDigit;
}
return Long.parseUnsignedLong(bankJoltage);
}
}