Day 2: Cube Conundrum

WebR is loading…

https://adventofcode.com/2023/day/2

Part 1

As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.

To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He’ll do this a few times per game.

You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: …) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).

For example, the record of a few games might look like this:

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 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

In game 1, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.

The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?

In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8.

Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?

Example Input Validation 1

sample1 = `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 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`

jsInputValidation1 = d3.sum(
  sample1
    .split("\n")
    .filter((d) => d != "")
    .map((d) => {
      const [game, draws] = d.split(": ");
      return {
        game: +game.replace(/[^\d]/g, ""),
        draws: draws.split("; ").map((draw) => {
          return draw
            .split(", ")
            .map((cubes) => {
              const [count, color] = cubes.split(" ");
              return [color, +count];
            })
            .reduce((obj, [key, value]) => {
              obj[key] = value;
              return obj;
            }, {});
        })
      };
    })
    .filter((g) => {
      return (
        d3.max(g.draws.map((c) => c.red)) <= 12 &&
        d3.max(g.draws.map((c) => c.green)) <= 13 &&
        d3.max(g.draws.map((c) => c.blue)) <= 14
      );
    })
    .map((g) => g.game)
)
rInputValidation1 = await webR.evalRNumber(`
r"(${sample1})" |> 
  stri_split_lines1() |> 
  lapply(\\(.game) {
    game_draw <- stri_split_fixed(.game, ": ")[[1]]
    game <- gsub("[^0-9]", "", game_draw[1])
    stri_match_all_regex(
      stri_split_fixed(game_draw[2], "; ")[[1]],
      "([0-9]+) ([a-z]+)"
    ) |> 
      lapply(\\(.draw) {
        setNames(as.integer(.draw[,2]), .draw[,3]) |> 
          as.list() |> 
          as.data.frame() |> 
          cbind.data.frame(data.frame(game = as.integer(game)))
      }) |> 
      dplyr::bind_rows()
  }) |> 
  dplyr::bind_rows() |> 
  dplyr::select(game, red, green, blue) |> 
  dplyr::group_by(game) |> 
  dplyr::summarise(
    max_red = max(red, na.rm = TRUE),
    max_green = max(green, na.rm = TRUE),
    max_blue = max(blue, na.rm = TRUE)
  ) |>  
  dplyr::filter(
    max_red <= 12 & max_green <= 13 & max_blue <= 14
  ) |> 
  dplyr::pull(game) |> 
  sum()
`)
jsInputValidation1 == rInputValidation1

Puzzle Input Question 1

jsPuzzleInput1 = d3.sum(
  puzzleInput
    .split("\n")
    .filter((d) => d != "")
    .map((d) => {
      const [game, draws] = d.split(": ");
      return {
        game: +game.replace(/[^\d]/g, ""),
        draws: draws.split("; ").map((draw) => {
          return draw
            .split(", ")
            .map((cubes) => {
              const [count, color] = cubes.split(" ");
              return [color, +count];
            })
            .reduce((obj, [key, value]) => {
              obj[key] = value;
              return obj;
            }, {});
        })
      };
    })
    .filter((g) => {
      return (
        d3.max(g.draws.map((c) => c.red)) <= 12 &&
        d3.max(g.draws.map((c) => c.green)) <= 13 &&
        d3.max(g.draws.map((c) => c.blue)) <= 14
      );
    })
    .map((g) => g.game)
)
rPuzzleInput1 = await webR.evalRNumber(`
r"(${puzzleInput})" |> 
  stri_split_lines1() |> 
  lapply(\\(.game) {
    game_draw <- stri_split_fixed(.game, ": ")[[1]]
    game <- gsub("[^0-9]", "", game_draw[1])
    stri_match_all_regex(
      stri_split_fixed(game_draw[2], "; ")[[1]],
      "([0-9]+) ([a-z]+)"
    ) |> 
      lapply(\\(.draw) {
        setNames(as.integer(.draw[,2]), .draw[,3]) |> 
          as.list() |> 
          as.data.frame() |> 
          cbind.data.frame(data.frame(game = as.integer(game)))
      }) |> 
      dplyr::bind_rows()
  }) |> 
  dplyr::bind_rows() |> 
  dplyr::select(game, red, green, blue) |> 
  dplyr::group_by(game) |> 
  dplyr::summarise(
    max_red = max(red, na.rm = TRUE),
    max_green = max(green, na.rm = TRUE),
    max_blue = max(blue, na.rm = TRUE)
  ) |>  
  dplyr::filter(
    max_red <= 12 & max_green <= 13 & max_blue <= 14
  ) |> 
  dplyr::pull(game) |> 
  sum()
`)
jsPuzzleInput1 == rPuzzleInput1

Part 2

The Elf says they’ve stopped producing snow because they aren’t getting any water! He isn’t sure why the water stopped; however, he can show you how to get to the water source to check it out for yourself. It’s just up ahead!

As you continue your walk, the Elf poses a second question: in each game you played, what is the fewest number of cubes of each color that could have been in the bag to make the game possible?

  • In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible.
  • Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
  • Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
  • Game 4 required at least 14 red, 3 green, and 15 blue cubes.
  • Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.

The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. The power of the minimum set of cubes in game 1 is 48. In games 2-5 it was 12, 1560, 630, and 36, respectively. Adding up these five powers produces the sum 2286.

For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?

jsInputValidation2 = d3.sum(
  sample1
    .split("\n")
    .filter((d) => d != "")
    .map((d) => {
      const [game, draws] = d.split(": ");
      return {
        game: +game.replace(/[^\d]/g, ""),
        draws: draws.split("; ").map((draw) => {
          return draw
            .split(", ")
            .map((cubes) => {
              const [count, color] = cubes.split(" ");
              return [color, +count];
            })
            .reduce((obj, [key, value]) => {
              obj[key] = value;
              return obj;
            }, {});
        })
      };
    })
    .map((g) => {
      return (
        d3.max(g.draws.map((c) => c.red)) *
        d3.max(g.draws.map((c) => c.green)) *
        d3.max(g.draws.map((c) => c.blue))
      );
    })
)
rInputValidation2 = await webR.evalRNumber(`
r"(${sample1})" |> 
  stri_split_lines1() |> 
  lapply(\\(.game) {
    game_draw <- stri_split_fixed(.game, ": ")[[1]]
    game <- gsub("[^0-9]", "", game_draw[1])
    stri_match_all_regex(
      stri_split_fixed(game_draw[2], "; ")[[1]],
      "([0-9]+) ([a-z]+)"
    ) |> 
      lapply(\\(.draw) {
        setNames(as.integer(.draw[,2]), .draw[,3]) |> 
          as.list() |> 
          as.data.frame() |> 
          cbind.data.frame(data.frame(game = as.integer(game)))
      }) |> 
      dplyr::bind_rows()
  }) |> 
  dplyr::bind_rows() |> 
  dplyr::select(game, red, green, blue) |> 
  dplyr::group_by(game) |> 
  dplyr::summarise(
    power = max(red, na.rm = TRUE) * 
      max(green, na.rm = TRUE) *
      max(blue, na.rm = TRUE)
  ) |> 
  dplyr::pull(power) |> 
  sum()
`)
jsInputValidation2 == rInputValidation2
jsPuzzleInput2 = d3.sum(
  puzzleInput
    .split("\n")
    .filter((d) => d != "")
    .map((d) => {
      const [game, draws] = d.split(": ");
      return {
        game: +game.replace(/[^\d]/g, ""),
        draws: draws.split("; ").map((draw) => {
          return draw
            .split(", ")
            .map((cubes) => {
              const [count, color] = cubes.split(" ");
              return [color, +count];
            })
            .reduce((obj, [key, value]) => {
              obj[key] = value;
              return obj;
            }, {});
        })
      };
    })
    .map((g) => {
      return (
        d3.max(g.draws.map((c) => c.red)) *
        d3.max(g.draws.map((c) => c.green)) *
        d3.max(g.draws.map((c) => c.blue))
      );
    })
)
rPuzzleInput2 = await webR.evalRNumber(`
r"(${puzzleInput})" |> 
  stri_split_lines1() |> 
  lapply(\\(.game) {
    game_draw <- stri_split_fixed(.game, ": ")[[1]]
    game <- gsub("[^0-9]", "", game_draw[1])
    stri_match_all_regex(
      stri_split_fixed(game_draw[2], "; ")[[1]],
      "([0-9]+) ([a-z]+)"
    ) |> 
      lapply(\\(.draw) {
        setNames(as.integer(.draw[,2]), .draw[,3]) |> 
          as.list() |> 
          as.data.frame() |> 
          cbind.data.frame(data.frame(game = as.integer(game)))
      }) |> 
      dplyr::bind_rows()
  }) |> 
  dplyr::bind_rows() |> 
  dplyr::select(game, red, green, blue) |> 
  dplyr::group_by(game) |> 
  dplyr::summarise(
    power = max(red, na.rm = TRUE) * 
      max(green, na.rm = TRUE) *
      max(blue, na.rm = TRUE)
  ) |> 
  dplyr::pull(power) |> 
  sum()
`)
jsPuzzleInput2 == rPuzzleInput2
puzzleInput = FileAttachment("/static/data/02-01.txt").text()
webR = {
  const { WebR, ChannelType } = await import(
    "https://cdn.jsdelivr.net/npm/webr/dist/webr.mjs"
  );

  const intWebR = new WebR({
    channelType: ChannelType.PostMessage,
    interactive: false
  });

  await intWebR.init();

  await intWebR.installPackages(["stringi", "dplyr"], true);
  await intWebR.evalRVoid(`suppressPackageStartupMessages({
library(stringi)
library(dplyr)
})`);

  document.querySelector("#loader").innerText = "🟢";
  document.querySelector("#msg").innerText = "WebR loaded!";

  return intWebR;
}