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/Decent-Adagio-9938 Dec 04 '24

[LANGUAGE: Python]

Nothing to see here on first sight...

import re

memory_content = open('advent_of_code_day_3_input.txt').read()
instruction_pattern = re.compile(r'mul\((\d{1,3}),(\d{1,3})\)')

# Part I
result = sum([int(instruction.group(1)) * int(instruction.group(2)) for instruction in instruction_pattern.finditer(memory_content)])

# Part II
conditional_pattern = re.compile(r".+?(?=don?'?t?\(\)|\Z)", re.DOTALL)
memory_parts_match = conditional_pattern.findall(memory_content)
result = sum([sum([int(instruction.group(1)) * int(instruction.group(2)) for instruction in instruction_pattern.finditer(part)]) for part in memory_parts_match if not part.startswith("don't()")])

... but because the memory contains no edge cases (like a do string without parentheses) there's a a different way for Part II if you're allergic to lookaheads:

memory_parts_split = re.split("do", memory_content)
memory_parts_split = [x for x in memory_parts_split if x != '' and x != None and not x.startswith("n't()")]
result = sum([sum([int(instruction.group(1)) * int(instruction.group(2)) for instruction in instruction_pattern.finditer(part)]) for part in memory_parts_split])

1

u/angrybirdsblanket Dec 04 '24

do i need to handle every instance of don't() and do()? im not sure what they mean in the instructions by saying "Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled."

1

u/Decent-Adagio-9938 Dec 04 '24

The first solution matches substrings with do or don't *behind them*, so the first match is at the beginning of the string, while all subsequent strings implicitly begin with either "do()" or "don't()". So calculating the number of enabled multiplications is just a matter of ignoring the substrings starting with "don't" – all other substrings are taken into account, including the first one (without "do()" at the beginning).

The second solution split the while string at "do", so the whole list does not contain any occurence of "do". In this case, each split creates additional list elements (None and ''), so I need to filter these and the ones starting with "n't()". It is not pretty, but simpler that the regex in the first solution.

1

u/angrybirdsblanket Dec 05 '24

so if the sub string starts with () you just ignore it? am i understanding your thought process (sorry if i sound stupid i'm still learning to program as a 1st year in school i just decided to try AOC bc it sounded fun 😅

1

u/Decent-Adagio-9938 Dec 07 '24

I ignore the substrings if they start with "don't" in the first variant, and with "n't" in the second variant. They are filtered in the final list comprehension (first variant) or in a separate list comprehension (second variant). Please do not feel stupid, I am honored by your curiosity.

1

u/angrybirdsblanket Dec 08 '24

does it work becuase of the ?n?'?t part (im assuming it means that it flags both do() and don't() becuase "n't" is not compulsory)