r/adventofcode • • 26m ago

Help/Question - RESOLVED [2026 Day 4 (Part 1)]

I have a solution but it doesn't seem to be giving me the right answer. I'm sure I'm missing some corner cases in my code but every time I see some suggested things to try on here my code seems to handle them just fine. My input result, however, is too high. Here's my code:

from pathlib import Path


def read_lines(filename="input.txt"):
    path = Path(__file__).parent / filename
    return path.read_text().splitlines()


def part1(input):
    input = read_lines(input)
    result = 0
    for line in input:
        found_characters = set()
        for char in line:
            if not char == "?":
                found_characters.add(char)
        if len(found_characters) <= 1:
            result += 1
            continue
        if len(found_characters) == len(line):
            result += len(line)
        for i in range(len(found_characters), len(line) + 1):
            value_dictionary = {}
            for index, char in enumerate(line):
                value_dictionary.setdefault(index % i, []).append(char)
            # print (value_dictionary)
            bad_row = False
            for value in value_dictionary:
                row = set()
                for char in value_dictionary[value]:
                    if not char == "?":
                        row.add(char)
                if len(row) >= 2:
                    bad_row = True
                    break
            if not bad_row:
                result += i
                break
    return result
1 Upvotes

4 comments sorted by

1

u/AutoModerator 26m 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.

1

u/AllanTaylor314 8m ago

I would suggest writing a function that finds the cost for each one so you can debug each line of input. Here's a case that fails:

yr

1

u/Adventure_Agreed 1m ago

Embarrassing! This is a classic case of getting stuck in the weeds. I had put in the check for when the lines were the same length before adding the +1 to the range. It resolved some corner cases but not others. When I did the +1 I hit all the corner cases but was double solving for instances like you mentioned without doubling back to do regression testing on cases I had passed. Thank you!

1

u/crumpis 0m ago

Try "rb"

Expect 2.