C++: Advent of Code — Day 2

Rud Merriam
One Stop C++
9 min readDec 23, 2023

--

This ongoing series explores functional programming (FP) and the C++ Ranges library, sometimes with some structured programming (SP) for added interest. It is an experiment to learn more about FP and the library. The first article, C++: Advent of Code Introduction, explains the purpose and introduces the framework used daily. Two articles describe Day 1’s, Part 1’s, and Part 2’s challenges. Today’s article covers both Parts 1 and 2 for Day 2.

The problem today turns out to be trivial in procedural code. Rather unusual, I finished both parts in a few hours. My background is in embedded control systems, so my experience is other than pattern-solving problems. But that’s another reason for me to work on these challenges. Since the solutions took only a short time, relatively, I worked on an FP solution for Part 2, which took a lot longer but was worthwhile.

Day 2 Problem

Today’s problem is based on a game. A bag has sets of red, blue, and green cubes. A random number of cubes are drawn from the bag., replaced, and a new set is drawn. This occurs a few times and makes a game. Here is a record of 5 games.

Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1…

--

--

Rud Merriam
One Stop C++

I am a retired software engineer with decades of experience with embedded systems and have used C++ since the early 90s.