|
61 | 61 | "\n",
|
62 | 62 | "get_calibration_sum(lines)"
|
63 | 63 | ]
|
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "## Day 2: Cube Conundrum\n", |
| 70 | + "\n", |
| 71 | + "What is the sum of the IDs of those games?" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": null, |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "def get_games(lines):\n", |
| 81 | + " games = []\n", |
| 82 | + " for game_str in [line.strip().split(':')[1].split(';') for line in lines]:\n", |
| 83 | + " subsets =[]\n", |
| 84 | + " for subset_str in game_str:\n", |
| 85 | + " subset = { 'blue': 0, 'green': 0, 'red': 0}\n", |
| 86 | + " for cube_str in subset_str.split(','):\n", |
| 87 | + " cubes = cube_str.split()\n", |
| 88 | + " subset[cubes[1]] = int(cubes[0])\n", |
| 89 | + " subsets.append(subset)\n", |
| 90 | + " games.append(subsets)\n", |
| 91 | + " return games\n", |
| 92 | + "\n", |
| 93 | + "def is_possible(subset):\n", |
| 94 | + " max_num_cubes = {'blue': 14, 'green': 13, 'red': 12}\n", |
| 95 | + " return all(subset[color] <= max_num_cubes[color] for color in max_num_cubes.keys())\n", |
| 96 | + "\n", |
| 97 | + "def get_possible_game_ids(games):\n", |
| 98 | + " possible_game_ids = []\n", |
| 99 | + " for game_id in range(len(games)):\n", |
| 100 | + " if all(is_possible(subset) for subset in games[game_id]):\n", |
| 101 | + " possible_game_ids.append(game_id+1)\n", |
| 102 | + " return possible_game_ids\n", |
| 103 | + "\n", |
| 104 | + "lines = open('02_input.txt', 'r').readlines()\n", |
| 105 | + "\n", |
| 106 | + "games = get_games(lines)\n", |
| 107 | + "sum(get_possible_game_ids(games))" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "What is the sum of the power of these sets?" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "from math import prod\n", |
| 124 | + "\n", |
| 125 | + "def get_powers(games):\n", |
| 126 | + " powers = []\n", |
| 127 | + " for game in games:\n", |
| 128 | + " min_cube_num = {'blue': 0, 'green': 0, 'red': 0}\n", |
| 129 | + " for subset in game:\n", |
| 130 | + " for color in subset:\n", |
| 131 | + " min_cube_num[color] = max(min_cube_num[color], subset[color])\n", |
| 132 | + " powers.append(prod(min_cube_num.values()))\n", |
| 133 | + " return powers\n", |
| 134 | + "\n", |
| 135 | + "sum(get_powers(games))" |
| 136 | + ] |
64 | 137 | }
|
65 | 138 | ],
|
66 | 139 | "metadata": {
|
|
0 commit comments