solved 3a

This commit is contained in:
2025-12-03 22:21:01 -06:00
parent c87b3ce367
commit 9fd5c2b616
2 changed files with 241 additions and 0 deletions

41
src/Problem3.java Normal file
View File

@@ -0,0 +1,41 @@
package src;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Problem3 {
public static void main(String[] args) {
String filePath = "inputs/inputProblem3.txt";
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
long total = 0;
while ((line = reader.readLine()) != null) {
total += maxTwoDigit(line);
}
System.out.println(total);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int maxTwoDigit(String digits) {
int maxDigit = -1;
int maxValue = 0;
for (char c : digits.toCharArray()) {
int digit = c - '0';
if (maxDigit >= 0) {
int value = maxDigit * 10 + digit;
if (value > maxValue) {
maxValue = value;
}
}
if (digit > maxDigit) {
maxDigit = digit;
}
}
return maxValue;
}
}