#Advent of Code day 1 part 2 (2023) what is the bug?

1 messages · Page 1 of 1 (latest)

glad birch
#

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

digital irisBOT
#

<@&987246399047479336> please have a look, thanks.

glad birch
#

!format

digital irisBOT
# glad birch !format

Looks like you attempted to use a command? Please note that we only use slash-commands on this server 🙂

Try starting your message with a forward-slash / and Discord should open a popup showing you all available commands.
A command might then look like /foo 👍

digital irisBOT
# glad birch public class Main { public static void main(String[] args) { String[...

Detected code, here are some useful tools:

[WARNING] The code couldn't end properly...

Problematic source code:


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```
Cause:
The code doesn't compile:
';' expected

## System out
[Nothing]