-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle1.java
91 lines (84 loc) · 2.2 KB
/
Puzzle1.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package advent2023;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.InputStream;
import java.util.List;
/**
* @author Éamonn McManus
*/
public class Puzzle1 {
public static void main(String[] args) throws Exception {
try (InputStream in = Puzzle1.class.getResourceAsStream("puzzle1.txt")) {
String lineString = new String(in.readAllBytes(), UTF_8);
List<String> lines = List.of(lineString.split("\n"));
System.out.printf("sum for part 1: %d\n", part1(lines));
System.out.printf("sum for part 2: %d\n", part2(lines));
}
}
private static int part1(List<String> lines) {
int sum = 0;
for (String line : lines) {
int digit1 = -1;
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (Character.isDigit(c)) {
digit1 = c - '0';
break;
}
}
assert digit1 > 0;
int digit2 = -1;
for (int i = line.length() - 1; i >= 0; i--) {
char c = line.charAt(i);
if (Character.isDigit(c)) {
digit2 = c - '0';
break;
}
}
assert digit2 > 0;
int n = 10 * digit1 + digit2;
sum += n;
}
return sum;
}
private static int part2(List<String> lines) {
int sum = 0;
for (String line : lines) {
int digit1 = -1;
for (int i = 0; i < line.length(); i++) {
int v = valueAt(line, i);
if (v >= 0) {
digit1 = v;
break;
}
}
assert digit1 > 0;
int digit2 = -1;
for (int i = line.length() - 1; i >= 0; i--) {
int v = valueAt(line, i);
if (v >= 0) {
digit2 = v;
break;
}
}
assert digit2 > 0;
int n = 10 * digit1 + digit2;
sum += n;
}
return sum;
}
private static final List<String> NUMBERS =
List.of("one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
private static int valueAt(String s, int i) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
return c - '0';
}
for (int j = 1; j < 10; j++) {
String word = NUMBERS.get(j - 1);
if (s.startsWith(word, i)) {
return j;
}
}
return -1;
}
}