Advent of Code 2023 - Day 2Cube Conundrum
| Problem statement | Source code | Tags: Data structures
Part 1
I parse each line as a list of lists, each list representing the count of R, G, and B cubes.
Figuring out how to index a structure like this is very, very difficult for me. It seems that lines[[1]][, 1] would return the R/G/B counts for the first round of the first game, etc., while lines[[1]]["red", ] would return the counts of red cubes for all rounds of the first game. Now I just need to check, for each line, if the count of each color and each round is in the expected range. The which function is very useful for this—especially since in R indices start from 1.
Part 2
For a game to be playable, the number of cubes must at least be the largest number drawn in a single round, which can be found by using max on line["red", ], line["green", ], and line["blue", ]. However because line["red", ] etc. are lists, I first need unlist to turn them into vectors.