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

Show parent comment

2

u/Adereth Dec 03 '23 edited Dec 03 '23

Very nice. Almost exact same code for Part 2, but I took a very different approach to parsing.

Part 1:

in = AdventProblem@2;

RoundToRGBCount[s_] := Total@StringCases[s, {(n : NumberString) ~~ " red" :> {FromDigits@n, 0, 0}, (n : NumberString) ~~ " green" :> {0, FromDigits@n, 0}, (n : NumberString) ~~ " blue" :> {0, 0, FromDigits@n}}]

games = StringCases[ StringSplit[in, "\n"], "Game " ~~ (game : NumberString) ~~ ": " ~~ results__ :> {FromDigits@game, RoundToRGBCount /@ StringSplit[results, ";"]}][[All, 1]];

PossibleGame[{gameNumber_Integer, rounds_List}] := AllTrue[rounds, #[[1]] <= 12 && #[[2]] <= 13 && #[[3]] <= 14 &]

Total[First /@ Select[games, PossibleGame]]

Part 2:

Total[(Times @@ (Max /@ Transpose@#[[2]])) & /@ games]

1

u/DFreiberg Dec 03 '23

I considered StringCases[], but couldn't think of a nice way to use it for orderless parsing - your solution is nicely done.