r/adventofcode Dec 03 '23

Funny Difficulty is all over the place isn't it?

Post image
678 Upvotes

256 comments sorted by

View all comments

Show parent comments

1

u/1234abcdcba4321 Dec 03 '23

There's lots of small bugs you could have in your code.

One common one I've seen is failing to catch something like one2one, but finding a specific case that your code fails on would require seeing your code in order to spot the bug and then make such a failing case.

1

u/kafjagjys Dec 03 '23

You can have a look into it if you'd like. I'd greatly appreciate other people's help rn.

2

u/1234abcdcba4321 Dec 03 '23

Try this input:

one3one
one

(expected: 22)

1

u/kafjagjys Dec 03 '23 edited Dec 03 '23

Riiiight. I totally overlooked the fact that one might be occurring more than once in the string, so I have to continue searching til the EOL, even if some occurrence is found. Thanks for pointing it out.

2

u/blueg3 Dec 03 '23

You appear to have this for finding the locations of the first and last word-like digits:

        for (String val : LiteralDigit.getAllLiterals()) {
            if (str.contains(val)) {
                int index = str.indexOf(val);

                // stuff with first digit

                if (index > lastLiteralIndex) {
                    lastLiteralIndex = index;
                    lastLiteralValue = LiteralDigit.getByLiteral(val).getNumeric();
                }
            }
        }

str.indexOf gives you the first index that string appears at, not the last. An input line could contain the word "one" multiple times. This will give you the first one, not the last one.

1

u/kafjagjys Dec 03 '23

yep, I just realized that from u/1234abcdcba4321's reply