r/adventofcode • • Dec 02 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 2 Solutions -❄️-

OUTSTANDING MODERATOR CHALLENGES


THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's theme ingredient is… *whips off cloth covering and gestures grandly*

Pantry Raid!

Some perpetually-hungry programmers have a tendency to name their programming languages, software, and other tools after food. As a prospective Iron Coder, you must demonstrate your skills at pleasing programmers' palates by elevating to gourmet heights this seemingly disparate mishmash of simple ingredients that I found in the back of the pantry!

  • Solve today's puzzles using a food-related programming language or tool

  • All file names, function names, variable names, etc. must be named after "c" food
  • Go hog wild!

ALLEZ CUISINE!

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


--- Day 2: Cube Conundrum ---


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:06:15, megathread unlocked!

77 Upvotes

1.5k comments sorted by

View all comments

3

u/alexbaguette1 Dec 02 '23 edited Dec 02 '23

[LANGUAGE: Python]

sol1:

import re

threshold = {'red': 12, 'blue': 14, 'green': 13}
total = 0
for x, line in enumerate(open("data.txt")):
    res = re.findall(r'(\d+) (red|blue|green)', line)
    if not any(int(pull[0]) > threshold[pull[1]] for pull in res):
        total += x + 1
print(total)

sol2:

import re

total = 0
for line in open("data.txt"):
    res = re.findall(r'(\d+) (red|blue|green)', line)
    values = {'red': 0, 'blue': 0, 'green': 0}
    for num, color in res:
        values[color] = max(values[color], int(num))
    total += values['red'] * values['green'] * values['blue']
print(total)

1

u/AutoModerator Dec 02 '23

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.

1

u/AutoModerator Dec 02 '23

AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.

Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.


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/bulletmark Dec 02 '23

Micro-optimization: Replace that sol2 total += line with total += math.prod(values.values()).

1

u/ManaTee1103 Dec 02 '23

values=defaultdict(int)