Skip to content

Commit a549ad7

Browse files
authored
Add solution for Day 1 in Java
1 parent 1323cda commit a549ad7

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

Advent of Code/2023/Day1.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import java.util.List;
2+
import java.util.Scanner;
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
import java.util.ArrayList;
6+
import java.io.File;
7+
8+
public class Day1 {
9+
public static void main(String[] args) {
10+
try {
11+
part1();
12+
part2();
13+
} catch (Exception e) {
14+
System.err.println("Error: " + e.getMessage());
15+
}
16+
}
17+
18+
/*
19+
* Process each line as characters, filter out those corresponding to digits
20+
*, and get the number formed by the first and last digits within the line.
21+
* If there is only 1 digit in the line, then use that digit twice.
22+
* That is the calibration value.
23+
*/
24+
private static Integer processLinePart1(String line) {
25+
List<Character> tokens = new ArrayList<Character>();
26+
for (int i = 0; i < line.length(); i++) {
27+
char ch = line.charAt(i);
28+
if (ch >= '0' && ch <= '9') {
29+
tokens.add(ch);
30+
}
31+
}
32+
if (line.length() > 1) {
33+
return Integer.parseInt("" + tokens.get(0) + tokens.get(tokens.size() - 1));
34+
} else {
35+
return Integer.parseInt("" + tokens.get(0) + tokens.get(0));
36+
}
37+
}
38+
39+
/*
40+
* Process each line using regular expressions and modify words to respective digits.
41+
* Get the number formed by the first and last digits within the line as in part 1.
42+
* If there is only 1 digit in the line, then use that digit twice.
43+
* That is the calibration value.
44+
*/
45+
private static Integer processLinePart2(String line) {
46+
Pattern pattern = Pattern.compile("(one|two|three|four|five|six|seven|eight|nine|\\d)");
47+
Matcher m = pattern.matcher(line);
48+
List<String> groups = new ArrayList<String>();
49+
while(m.find()) {
50+
String group = m.group(1);
51+
if (Pattern.compile("\\d").matcher(group).matches()) {
52+
groups.add(group);
53+
} else {
54+
switch(group) {
55+
case "one": groups.add("1");
56+
break;
57+
case "two": groups.add("2");
58+
break;
59+
case "three": groups.add("3");
60+
break;
61+
case "four": groups.add("4");
62+
break;
63+
case "five": groups.add("5");
64+
break;
65+
case "six": groups.add("6");
66+
break;
67+
case "seven": groups.add("7");
68+
break;
69+
case "eight": groups.add("8");
70+
break;
71+
case "nine": groups.add("9");
72+
break;
73+
}
74+
}
75+
}
76+
if (line.length() > 1) {
77+
return Integer.parseInt("" + groups.get(0) + groups.get(groups.size() - 1));
78+
} else {
79+
return Integer.parseInt("" + groups.get(0) + groups.get(0));
80+
}
81+
}
82+
83+
public static void part1() throws Exception {
84+
Scanner sc = new Scanner(new File("test.in"));
85+
int sum = 0;
86+
while(sc.hasNextLine()) {
87+
String line = sc.nextLine();
88+
int calibrationValue = processLinePart1(line);
89+
sum += calibrationValue;
90+
}
91+
sc.close();
92+
System.out.println(sum);
93+
}
94+
95+
public static void part2() throws Exception {
96+
Scanner sc = new Scanner(new File("test.in"));
97+
int sum = 0;
98+
99+
while(sc.hasNextLine()) {
100+
String line = sc.nextLine();
101+
int calibrationValue = processLinePart2(line);
102+
sum += calibrationValue;
103+
}
104+
sc.close();
105+
System.out.println(sum);
106+
}
107+
}

0 commit comments

Comments
 (0)