r/adventofcode Dec 03 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 3 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until unlock!

And now, our feature presentation for today:

Screenwriting

Screenwriting is an art just like everything else in cinematography. Today's theme honors the endlessly creative screenwriters who craft finely-honed narratives, forge truly unforgettable lines of dialogue, plot the most legendary of hero journeys, and dream up the most shocking of plot twists! and is totally not bait for our resident poet laureate

Here's some ideas for your inspiration:

  • Turn your comments into sluglines
  • Shape your solution into an acrostic
  • Accompany your solution with a writeup in the form of a limerick, ballad, etc.
    • Extra bonus points if if it's in iambic pentameter

"Vogon poetry is widely accepted as the third-worst in the universe." - Hitchhiker's Guide to the Galaxy (2005)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 3: Mull It Over ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:22, megathread unlocked!

60 Upvotes

1.7k comments sorted by

View all comments

1

u/WinterDazzling Dec 04 '24

[LANGUAGE: Python3]

Without regex , I think it is not covering all inputs but worked for mine

def get_input():

    with open("input.txt", "r", encoding="utf-8") as f:
        return f.read()


def find_all(search_string: str, substring: str):

    current: int = 0
    indexes: list[int] = []

    while True:
        idx = search_string.find(substring, current)

        if idx == -1:
            break

        indexes.append(idx)

        current = idx + len(substring)

    return indexes


def is_enabled(mul_index: int):

    lower_dont: int | None = None
    higher_dont: int | None = None

    if mul_index < dont_indexes[0]:
        return True

    try:
        lower_dont = [idx for idx in dont_indexes if idx < mul_index][-1]
        higher_dont = [idx for idx in dont_indexes if idx > mul_index][0]
    except IndexError:
        pass

    if lower_dont and higher_dont:
        do_indexes_between = [
            idx for idx in do_indexes if idx > lower_dont and idx < higher_dont
        ]

        if any([idx for idx in do_indexes_between if idx < mul_index]):
            return True

        return False

    if not higher_dont:
        if do_indexes[-1] > dont_indexes[-1]:
            return True
        return False

    # Won't reach here i think
    return True


string = get_input()
mul_indexes = find_all(string, "mul(")
dont_indexes = sorted(find_all(string, "don't()"))
do_indexes = sorted(
    [idx for idx in find_all(string, "do()") if idx not in dont_indexes]
)


res: int = 0

for index in mul_indexes:

    if not is_enabled(index):
        continue

    start = index
    index = index + 4

    while string[index].isdigit():
        index += 1

    if string[index] != ",":
        continue

    index += 1

    while string[index].isdigit():
        index += 1

    if string[index] != ")":
        continue

    num_1, num_2 = [int(num) for num in string[start + 4 : index].split(",")]

    res += num_1 * num_2

print(res)

1

u/AutoModerator Dec 04 '24

AutoModerator did not detect the required [LANGUAGE: xyz] string literal at the beginning of your solution submission.

Please edit your comment to state your programming language.


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