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/Different-Ease-6583 Dec 02 '23

[LANGUAGE: Python]

import Utils
from aocd import submit

day = 2
year = 2023
p1_expected_tst_result = 8
p2_expected_tst_result = 2286

Utils.download_input(year, day)

def is_good_pull(pull):
    for color in pull:
        if cubes[color[1]] < int(color[0]):
            return False
    return True

def is_good_game(pulls):
    for pull in pulls:
        if not is_good_pull(pull):
            return False

    return True

def parse_games(data):
    return [
        {
            "game_id": int(game[0]),
            "pulls": [list(map(str.split, pull.split(", "))) for pull in game[1].split("; ")]
        }
        for game in (Utils.extract_string("Game %: %", line) for line in data)
    ]

def solve(data):
    return sum(game['game_id'] for game in parse_games(data) if is_good_game(game['pulls']))

def solve2(data):
    games = parse_games(data)

    total = 0
    for game in games:
        minimum_cubes = {"red": 0, "green": 0, "blue": 0}
        for pull in game["pulls"]:
            for color in pull:
                if int(color[0]) > minimum_cubes[color[1]]:
                    minimum_cubes[color[1]] = int(color[0])

        total += minimum_cubes["red"] * minimum_cubes["green"] * minimum_cubes["blue"]

    return total

tst_input = Utils.read_input(f"input/day{day}_tst_input.txt")
puzzle_input = Utils.read_input(f"input/day{day}_input.txt")

cubes = {
    "red": 12,
    "green": 13,
    "blue": 14
}
→ More replies (1)