public class Main {
public static void main(String[] args) {
String[] rows = ManasUtils.fileToString("Books.txt");
int sum = 0;
int i = 0;
for (String row: rows){
sum+= getDigit(row);
System.out.println("ROW NO: "+ (i+1) + " DIGIT FOUND: "+getDigit(row));
i++;
}
System.out.println(sum);
}
public static int getDigit(String row) {
boolean firstDigitfound = false;
int firstDigit = 0;
int lastDigit = 0;
String toCheckForNumber = "";
for (int i = 0; i < row.length(); i++) {
char c = row.charAt(i);
toCheckForNumber += c;
int number = checkForNumber(toCheckForNumber);
if (number!=-1){
toCheckForNumber = "";
if (!firstDigitfound){
firstDigit = number;
firstDigitfound = true;
}
lastDigit = number;
}
if (Character.isDigit(c)) {
if (!firstDigitfound) {
firstDigit = Character.getNumericValue(c);
firstDigitfound = true;
}
lastDigit = Character.getNumericValue(c);
}
}
int number = lastDigit + firstDigit * 10;
return number;
}
public static int checkForNumber(String toCheck) {
String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
for (String number: numbers){
if (toCheck.contains(number))
return ManasUtils.nameToNumber(number);
}
return -1;
}
}
i know its not the most efficient, but the answer it's giving is too low for some reason, it works with the test data but not on the real data
https://adventofcode.com/2023/day/1 here is the question