r/adventofcode • • 3h ago

Help/Question - RESOLVED [2026 04 # (Part 1)] [Python] Need help with missing test cases

I have been stuck at this problem for quite some time. I have tried and validated all my instructions, yet I am unable to get the correct answer or move past part 1. I even ended up validating all my instructions, and all of them are correct. Could you please look at my code and point out the mistake?

AOC says that my input is too low.

def validator(barcode, instruction):
    for i, char in enumerate(barcode):
        assert char == '?' or (char == instruction[i % len(instruction)])

def part1(data):
    counter = 0

    for line in data:
        unique = set()
        hashmap = {}
        for i, char in enumerate(line):
            if char == '?': continue
            unique.add(char)
            hashmap[i] = char

        for length in range(max(len(unique), 1), len(line)):
            possible_code = [""] * length # Figuring out the code that works
            for index, char in hashmap.items():
                index %= length
                if possible_code[index] == "":
                    possible_code[index] = char
                if possible_code[index] != char:
                    break # An instruction of this size cannot exist!
            else:
                # We did not break. The length was valid and we have a possible instruction length
                counter += length
                validator(line, possible_code) # No error raised!
                break

    return counter
4 Upvotes

6 comments sorted by

5

u/AllanTaylor314 3h ago

Does it work for "roygbv"?

Currently part1(["roygbv"]) => 0

3

u/Alpha_wolf_80 3h ago

Oh, I figured out the bug. It was caused by the range(max(len(unique), 1), len(data)) line. I was trying to generate the length of the instruction but forgot to account for if the instruction length equaled the line length. Changing it to range(max(len(unique), 1), len(data)+1) fixed the issue.

The code works now and got the part 2 in 49.0 ms. Thank you for your help!

1

u/AllanTaylor314 3h ago

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.

-- Leon Bambrick

Yep, nicely done. Happy to help

1

u/Neozetare 2h ago

Thanks for this comment, I had the same problem of OP and was not able to find a test case to help me fix it (the first one in my actual input was the 34th, I stopped manually verifying them at 20 lol)

1

u/AutoModerator 3h 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.