-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.java
36 lines (30 loc) · 844 Bytes
/
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
public class Day1 {
public static int part1(String input) {
byte[] bytes = input.getBytes();
byte first = bytes[0];
byte prev = first;
int result = 0;
for (int i = 1; i < bytes.length; i++) {
byte c = bytes[i];
if(prev == c)
result += c - '0';
prev = c;
}
if(first == bytes[bytes.length-1]) {
result += (first-'0');
}
return result;
}
public static int part2(String input) {
byte[] bytes = input.getBytes();
int result = 0;
int half = bytes.length/2;
for (int i = 0; i < half; i++) {
byte c = bytes[i];
byte next = bytes[i+half];
if(next == c)
result += c - '0';
}
return result*2;
}
}