10000 2023: Add day 2. · HoffmannChristian/adventofcode@44ae9b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 44ae9b3

Browse files
2023: Add day 2.
1 parent 6a815bd commit 44ae9b3

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

2023/advent_of_code_2023.ipynb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,79 @@
6161
"\n",
6262
"get_calibration_sum(lines)"
6363
]
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+
]
64137
}
65138
],
66139
"metadata": {

0 commit comments

Comments
 (0)
0