-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.java
46 lines (43 loc) · 1.53 KB
/
Day1.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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Day1 {
public static int matchingDigitsSum(String puzzleInput) {
char[] inputDigits = puzzleInput.toCharArray();
int sum = 0;
int i, j;
// check each adjacent pair of digits for equality
for (int k = 0; k < inputDigits.length; k++) {
i = Character.getNumericValue(inputDigits[k % inputDigits.length]);
j = Character.getNumericValue(
inputDigits[(k + 1) % inputDigits.length]
);
if (i == j) {
sum += i;
}
}
return sum;
}
public static int halfwaySum(String puzzleInput) {
char[] inputDigits = puzzleInput.toCharArray();
int sum = 0;
int i, j;
// check digits halfway around the circular list for equality
for (int k = 0; k < inputDigits.length; k++) {
i = Character.getNumericValue(inputDigits[k % inputDigits.length]);
j = Character.getNumericValue(
inputDigits[(k + inputDigits.length/2) % inputDigits.length]
);
if (i == j) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) throws IOException {
String contents = new String(Files.readAllBytes(Paths.get("1.txt")));
contents = contents.trim();
System.out.println(matchingDigitsSum(contents));
System.out.println(halfwaySum(contents));
}
}