Skip to content

2023 - Day 2 - Cube Conundrum

Posted on:December 2, 2023 at 09:00 AM

A High-Flying Cube Puzzle

The new year of Advent of Code begins with a numeric puzzle: a calibration document altered by an enthusiastic young elf. Our goal? To decipher calibration values hidden within lines of text and calculate their sum.

Our Solution

We split our solution into two functions, partOne and partTwo, with a shared utility function to minimize code duplication. Here’s how we tackled it using TypeScript.

Shared Utility: getColorAmount

function maxAmount(colors: string[]): number {
  return Math.max(...colors.map(color => parseInt(color.split(" ")[0])));
}

export default function getColorAmount(color: string, game: string): number {
  const match = game.match(new RegExp(`\\d+(?: ${color})`, "g"));
  return match ? maxAmount(match) : 0;
}

Our utility function, getColorAmount, streamlined the process of extracting and determining the maximum amount of each color per game, ensuring efficiency and reducing code repetition.

Part One: Maximum Cube Calculations

import getColorAmount from "./utils";

function isPossible(color: "red" | "green" | "blue", amount: number): boolean {
  const maxAmounts = { red: 12, green: 13, blue: 14 };
  return amount <= maxAmounts[color];
}

export default function partOne(games: string[]): number {
  return games.reduce((acc, game, index) => {
    const numReds = getColorAmount("red", game);
    const numGreens = getColorAmount("green", game);
    const numBlues = getColorAmount("blue", game);

    const redPossible = isPossible("red", numReds);
    const greenPossible = isPossible("green", numGreens);
    const bluePossible = isPossible("blue", numBlues);

    const possible = redPossible && greenPossible && bluePossible;
    if (possible) acc += index + 1;

    return acc;
  }, 0);
}

In partOne, we focused on identifying which games could be possible with a specific number of red, green, and blue cubes. We utilized a utility function to determine the maximum number of each color revealed per game and then compared it against the given limits.

Part Two: Power of Minimum Sets

import getColorAmount from "./utils";

export default function partTwo(games: string[]): number {
  return games.reduce((acc, game) => {
    const numReds = getColorAmount("red", game);
    const numGreens = getColorAmount("green", game);
    const numBlues = getColorAmount("blue", game);

    return (acc += numReds * numGreens * numBlues);
  }, 0);
}

For partTwo, we calculated the minimum set of cubes required for each game to be possible and then summed up their “power,” defined as the product of the numbers of each color in these minimum sets.

Complexity Analysis

Both partOne and partTwo iterate through each game, with additional operations for string processing and arithmetic calculations. The complexity is primarily O(n), where n is the number of games.

Conclusion

This day’s challenge tested our ability to manipulate and analyze string data to deduce numeric values, showcasing the power of TypeScript in problem-solving. With clever coding and strategic thinking, we unraveled the cube mystery on Snow Island!

Stay tuned as we continue to navigate through more Advent of Code challenges!