#๐Ÿ”’ Help with puzzle answer

12 messages ยท Page 1 of 1 (latest)

charred venture
#

This is the Day 2 puzzle from Advent of Code. I realize I'm quite late, but I've started working on the puzzles. I would appreciate it if someone could confirm whether my solution is correct (I was able to solve the puzzle and obtain the right answers, but I still want to make sure I did things the correct way).

Part 1:

Since the young Elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs.

Part 2:

Now, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So, 12341234 (1234 two times), 123123123 (123 three times), 1212121212 (12 five times), and 1111111 (1 seven times) are all invalid IDs.

What both want:

part 1 : What do you get if you add up all of the invalid IDs?
part 2 : What do you get if you add up all of the invalid IDs using these new rules?
eager mauveBOT
#

@charred venture

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

charred venture
#
# Puzzle 2 Completely Solved By Ra.

# The Function For Validating a single ID.
def validate_id_part_1(ident : str | int):
    strid = str(ident)
    first_half = str(strid[:int((len(strid) + 1) / 2)])
    second_half = str(strid[int((len(strid) + 1) / 2):])
    if first_half == second_half:
        return int(strid)
    
def validate_id_part_2(ident : str | int):
    invalid = False
    strid = str(ident) # "123123123"
    for i in range(len(strid)):
        # Split the string to sequences of digits and check if the sequence is repeated the same way through the whole string at least twice.
        if strid.count(strid[:i+1]) * strid[:i+1] == strid and len(strid) > 1  and strid[:i+1] != strid:
            invalid = True
            break
    if invalid:
        return int(strid)

    
def solve_puzzle2_part1():
    total_invalid_ids = 0
    with open("puzzle2nput.txt", 'r') as file:
        idranges = file.read()
        idranges_list = idranges.split(",")
    for idrange in idranges_list:
        ids = []
        start, end = idrange.split("-")
        for i in range(int(start), int(end) + 1):
            ids.append(i)
        for ident in ids:
            if validate_id_part_1(ident):
                total_invalid_ids += ident

    print(f"Part 1 :Adding All Invalid IDS Up Gives : {total_invalid_ids}")


def solve_puzzle2_part2():
    total_invalid_ids = 0
    with open("puzzle2nput.txt", 'r') as file:
        idranges = file.read()
        idranges_list = idranges.split(",")
    for idrange in idranges_list:
        ids = []
        start, end = idrange.split("-")
        for i in range(int(start), int(end) + 1):
            ids.append(i)
        for ident in ids:
            if validate_id_part_2(ident):
                total_invalid_ids += ident

    print(f"Part 2 :Adding All Invalid IDS Up Gives : {total_invalid_ids}")
                    


if __name__ == "__main__":
    solve_puzzle2_part1()
    solve_puzzle2_part2()
high bone
#

If you got the correct answers, then it seems you did things correctly. ๐Ÿ™‚

charred venture
#

Maybe I would like to know if there's a better way, because mine might be a bad solution but gave a correct answer.

#

and the flaws with my solution, that's why I decided to make this post

high bone
#

Well, we do have a channel for this if you'd like to browse through other folks' solutions:
#1047673173447020564 , and then locate the topic for this day.

charred venture
#

alright

high bone
#

I will offer that creating a list of ids is not really necessary. It expands the memory footprint unnecessarily, and you end up not needing most of those numbers.
You could simply validate the i that you're working on at the moment, then add it to the total if it's valid.

charred venture
#

oh alright thank you for helping

eager mauveBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.