This is Day 2 of Advent of Code 2023! If you would like to solve the problems before looking at the solution, you can find them here.

In part 1 of Day 2, we're parsing some strings with some regexes! The problem is pretty simple - we've been given a file with games on each line and each game consisting of a number of sets and each set consisting of a number of red, green, or blue cubes. These cubes come out of a bag which has a fixed number of cubes for each color, and we need to figure out which games have no more than the allowed number of cubes for each color. We use a `split()` followed by some cleaning and parsing to obtain the game ID as an integer. The second part of the split contains the game records which are parsed using regexes. I created a different regex for each color, but I assume there's a cleaner way to do this.

The regex used for the color red is `"([0-9]+) red"`. Let's take a look at what that does.

- ►
`([0-9]+)`This is a capturing group that matches one or more digits (0-9). The square brackets denote a character class, and the 0-9 inside it means any digit from 0 to 9. The + quantifier means "one or more occurrences." This group captures the numeric part of the pattern. - ►
`red`This part simply matches the literal string " red" (with a space before "red").

In summary, this regular expression is looking for a pattern where there is a sequence of one or more digits followed by the space and the word "red.". I did the same parsing for green and blue, and recording for each line if the number of red, blue, or green cubes was less than or equal to the allowed numbers. Summing both up the IDs for valid games gives us our answer for part 1. Click here to jump to the part 2 discussion.

For part 2 of Day 1, we're looking at the problem from a slightly different angle. Instead of comparing against the allowed number, we're simply storing the maximum values, which would be the minimum number of cubes required for a valid game. The answer to this problem is the sum of the product of the maximum red, green, and blue values per line.

That's all folks!

Cheers,

Devang