DayOneAndTwo
This commit is contained in:
@ -39,15 +39,198 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 54,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2545\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def fileread(file):\n",
|
||||
" with open(file, 'r') as read:\n",
|
||||
" for line in read:\n",
|
||||
" yield line\n",
|
||||
"\n",
|
||||
"class games:\n",
|
||||
"\n",
|
||||
" def __init__(self, game):\n",
|
||||
" self.red = []; self.green = []; self.blue = []\n",
|
||||
" self.gameID = 0\n",
|
||||
"\n",
|
||||
" game = game.split(':')\n",
|
||||
" self.gameID = int(game[0].replace('Game', ''))\n",
|
||||
" game = game[1].split(';')\n",
|
||||
"\n",
|
||||
" for i in game:\n",
|
||||
" round = i.split(',')\n",
|
||||
" for ii in round:\n",
|
||||
" if 'green' in ii:\n",
|
||||
" self.green.append(int(ii.replace('green', '')))\n",
|
||||
" elif 'blue' in ii:\n",
|
||||
" self.blue.append(int(ii.replace('blue', '')))\n",
|
||||
" elif 'red' in ii:\n",
|
||||
" self.red.append(int(ii.replace('red', '')))\n",
|
||||
" \n",
|
||||
" def possible(self, tred, tgreen, tblue):\n",
|
||||
" for r in self.red:\n",
|
||||
" if r > tred:\n",
|
||||
" return False\n",
|
||||
" for g in self.green:\n",
|
||||
" if g > tgreen:\n",
|
||||
" return False\n",
|
||||
" for b in self.blue:\n",
|
||||
" if b > tblue:\n",
|
||||
" return False\n",
|
||||
" return True\n",
|
||||
"\n",
|
||||
"readl = fileread('games.txt')\n",
|
||||
"\n",
|
||||
"Result = 0\n",
|
||||
"while True:\n",
|
||||
" c = next(readl, None)\n",
|
||||
" if c is None:\n",
|
||||
" break\n",
|
||||
" obj = games(c)\n",
|
||||
" if obj.possible(12, 13, 14):\n",
|
||||
" Result += obj.gameID\n",
|
||||
"\n",
|
||||
"print(Result)\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"--- Part Two ---\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"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!\n",
|
||||
"\n",
|
||||
"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?\n",
|
||||
"\n",
|
||||
"Again consider the example games from earlier:\n",
|
||||
"\n",
|
||||
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green\n",
|
||||
"\n",
|
||||
"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue\n",
|
||||
"\n",
|
||||
"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red\n",
|
||||
"\n",
|
||||
"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red\n",
|
||||
"\n",
|
||||
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"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.\n",
|
||||
"\n",
|
||||
"Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.\n",
|
||||
"\n",
|
||||
"Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.\n",
|
||||
"\n",
|
||||
"Game 4 required at least 14 red, 3 green, and 15 blue cubes.\n",
|
||||
"\n",
|
||||
"Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.\n",
|
||||
"\n",
|
||||
"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.\n",
|
||||
"\n",
|
||||
"For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"78111\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def fileread(file):\n",
|
||||
" with open(file, 'r') as read:\n",
|
||||
" for line in read:\n",
|
||||
" yield line\n",
|
||||
"\n",
|
||||
"class games:\n",
|
||||
"\n",
|
||||
" def __init__(self, game):\n",
|
||||
" self.red = []; self.green = []; self.blue = []\n",
|
||||
" self.gameID = 0\n",
|
||||
"\n",
|
||||
" game = game.split(':')\n",
|
||||
" self.gameID = int(game[0].replace('Game', ''))\n",
|
||||
" game = game[1].split(';')\n",
|
||||
"\n",
|
||||
" for i in game:\n",
|
||||
" round = i.split(',')\n",
|
||||
" for ii in round:\n",
|
||||
" if 'green' in ii:\n",
|
||||
" self.green.append(int(ii.replace('green', '')))\n",
|
||||
" elif 'blue' in ii:\n",
|
||||
" self.blue.append(int(ii.replace('blue', '')))\n",
|
||||
" elif 'red' in ii:\n",
|
||||
" self.red.append(int(ii.replace('red', '')))\n",
|
||||
" \n",
|
||||
" def possible(self, tred, tgreen, tblue):\n",
|
||||
" for r in self.red:\n",
|
||||
" if r > tred:\n",
|
||||
" return False\n",
|
||||
" for g in self.green:\n",
|
||||
" if g > tgreen:\n",
|
||||
" return False\n",
|
||||
" for b in self.blue:\n",
|
||||
" if b > tblue:\n",
|
||||
" return False\n",
|
||||
" return True\n",
|
||||
" \n",
|
||||
" def powerofset(self):\n",
|
||||
" minr = max(self.red)\n",
|
||||
" ming = max(self.green)\n",
|
||||
" minb = max(self.blue)\n",
|
||||
"\n",
|
||||
" return minr * ming * minb\n",
|
||||
"\n",
|
||||
"readl = fileread('games.txt')\n",
|
||||
"\n",
|
||||
"Result = 0\n",
|
||||
"while True:\n",
|
||||
" c = next(readl, None)\n",
|
||||
" if c is None:\n",
|
||||
" break\n",
|
||||
" obj = games(c)\n",
|
||||
" Result += obj.powerofset()\n",
|
||||
"\n",
|
||||
"print(Result)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.12.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
Reference in New Issue
Block a user