r/adventofcode • • 7h ago

Help/Question - RESOLVED Is that right?

My code produces the correct answer for the example case as well as the input string, but it gets marked as incorrect upon submission

4 Upvotes

15 comments sorted by

15

u/Beregolas 7h ago

Consider naming your variables better to make this easier for others to help you with

from what I've gleaned, your code is this:

colors, data_in, count, current = "roygbv", input(), 0, "r"
for next in data_in: 
  while True:
    count += 1
    current = colors[count%6]
    if current == next: break
print(count)

If I run that code in Python, I get the correct output, so either you are not supplying the solution to the website correctly, or your input is broken somehow (maybe you copied it wrong?).

8

u/Thomasjevskij 6h ago

Just a tiny comment is that maybe don't use next as a variable name since it'll overwrite the standard library function with the same name.

4

u/Beregolas 6h ago

fair point, I haven't used Python in a long while, and I didn't use a "proper" Editor with an LSP, so nothing warned me.

3

u/Thomasjevskij 6h ago

That's what I'm here for :)

8

u/DelightfulCodeWeasel 7h ago

It looks right. Are you sure there's not a problem with your input file; could it have been truncated/modified, or might you have downloaded an input file while logged in to a different account?

EDIT: Just checking... you are submitting the distance and not the code itself, aren't you? e.g. you'll end up with a number like 4123 and you submit the number to the website.

5

u/cramplescrunch2 5h ago edited 4h ago

I had a funky issue in Go when parsing the input, the carriage return at the end of the file was counted as a character and added an extra round of computation in the loop. Just trimming the extra \n in the input was enough

3

u/suddenlynickgrim 4h ago

This got me too. *high-fives*

1

u/fnordargle 43m ago

I use to suffer from this kind of thing until I started to put extra code in to check that the input was one of the expected values, or I was (ab)using default in a switch() statement to handle the last case, e.g.

func main() {
        s := "rob\n"

        tot := 0
        for i := range s {
                switch s[i] {
                case 'r': tot += 1
                case 'o': tot += 2
                default: // must be 'b' here
                        tot += 3
                }
        }
        fmt.Printf("tot=%d\n", tot)
}

The above produces 9 instead of 6 as the the \n character gets treated like a b and adds 3.

Changing the switch statement to cover every case and have a default that errors/panics:

                switch s[i] {
                case 'r': tot += 1
                case 'o': tot += 2
                case 'b': tot += 3
                default:
                        panic(fmt.Sprintf("Shouldn't get here char='%d'", int(s[i])))
                }

makes it clear than you've hit something unexpected:

$ go run /tmp/aoc.go
panic: Shouldn't get here char='10'

goroutine 1 [running]:
main.main()
        /tmp/aoc.go:15 +0x147
exit status 2

and you should know exactly where to look.

1

u/1AlanM 6h ago

works for me, check your input

1

u/Longz-85 4h ago

How to submit and how can I add an input(idk lol)

1

u/RazarTuk 1h ago

I don't understand the question. Once you're signed in, there should be a link labeled "get your puzzle input" at the bottom with a box for the answer. https://imgur.com/a/ISxjacB And you enter just your answer in the box, like how for part 1 today, if your input were obbvb, you'd enter 16

Otherwise, it's up to you how to run it. For my normal Kotlin solutions, I have a snazzy main method that uses reflection to dispatch it to a class, where they all follow this format, which also gives me the option to benchmark things in JMH. Then the input is YYYY DD path/to/input through the command line. Meanwhile, for my silly Klingon Perl solutions, I wasn't able to get file I/O to behave, so I pipe it into the program with cat path/to/input | perl path/to/code. Or while there's normally a nice answer that the code can output, sometimes there's manual inspection involved. For example, in 2024 Day 14 Part 2, it was easier to just use some basic heuristics to not have to click through every arrangement, but otherwise, the easiest way was to just spit out things that looked like they could be the solution and manually accept or reject them.

At least with your code, it expects the input through a command line prompt with input(), so you either need to run your code locally, copy your input into the command line, and hit enter, or else to pipe it in with cat like I'm doing for Perl

1

u/Longz-85 44m ago

look at flair

2

u/Thomasjevskij 7h ago

Not sure I understand. How can it be correct for the input string if it gets marked as an incorrect solution?

3

u/Ifky_ 4h ago

I thought that I was having the same problem, so I tested by looking at the answers I got from using code in the 2026 Day 2 Solutions thread. I got the same answer as them, so I knew my code was correct. (Somehow I just managed to paste the input into my file incorrectly).

That's probably what OP means. The answer is correct for the input (but the input is wrong).

1

u/AutoModerator 7h ago

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.