DayOneAndTwo
This commit is contained in:
@ -61,14 +61,14 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" if c is None:\n",
|
" if c is None:\n",
|
||||||
" break\n",
|
" break\n",
|
||||||
" sublist = []\n",
|
" front = []\n",
|
||||||
"\n",
|
"\n",
|
||||||
" for i in c:\n",
|
" for i in c:\n",
|
||||||
" if i in numbers:\n",
|
" if i in numbers:\n",
|
||||||
" sublist.append(str(i)) \n",
|
" front.append(str(i)) \n",
|
||||||
" \n",
|
" \n",
|
||||||
" if len(numbers) > 1:\n",
|
" if len(numbers) > 1:\n",
|
||||||
" Solution += int(sublist[0] + sublist[len(sublist)-1])\n",
|
" Solution += int(front[0] + front[len(front)-1])\n",
|
||||||
" \n",
|
" \n",
|
||||||
" elif len(numbers) == 1:\n",
|
" elif len(numbers) == 1:\n",
|
||||||
" Solution += int(numbers[0]*2)\n",
|
" Solution += int(numbers[0]*2)\n",
|
||||||
@ -82,6 +82,112 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"The right answer is: 55447"
|
"The right answer is: 55447"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"--- Part Two ---\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid \"digits\".\n",
|
||||||
|
"\n",
|
||||||
|
"Equipped with this new information, you now need to find the real first and last digit on each line. For example:\n",
|
||||||
|
"\n",
|
||||||
|
"two1nine\n",
|
||||||
|
"eightwothree\n",
|
||||||
|
"abcone2threexyz\n",
|
||||||
|
"xtwone3four\n",
|
||||||
|
"4nineeightseven2\n",
|
||||||
|
"zoneight234\n",
|
||||||
|
"7pqrstsixteen\n",
|
||||||
|
"In this example, the calibration values are 29, 83, 13, 24, 42, 14, and 76. Adding these together produces 281.\n",
|
||||||
|
"\n",
|
||||||
|
"What is the sum of all of the calibration values?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 47,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"54706\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def openafile(filename):\n",
|
||||||
|
" with open(filename, 'r') as file:\n",
|
||||||
|
" for line in file:\n",
|
||||||
|
" yield line.strip()\n",
|
||||||
|
"\n",
|
||||||
|
"def lookfornum(lines, letter, ir):\n",
|
||||||
|
" line = ''\n",
|
||||||
|
" for i in lines[:lines.index(letter)]:\n",
|
||||||
|
" line += i\n",
|
||||||
|
" if 'one'[::ir] in line:\n",
|
||||||
|
" return 1\n",
|
||||||
|
" elif 'two'[::ir] in line:\n",
|
||||||
|
" return 2\n",
|
||||||
|
" elif 'three'[::ir] in line:\n",
|
||||||
|
" return 3\n",
|
||||||
|
" elif 'four'[::ir] in line:\n",
|
||||||
|
" return 4\n",
|
||||||
|
" elif 'five'[::ir] in line:\n",
|
||||||
|
" return 5\n",
|
||||||
|
" elif 'six'[::ir] in line:\n",
|
||||||
|
" return 6\n",
|
||||||
|
" elif 'seven'[::ir] in line:\n",
|
||||||
|
" return 7\n",
|
||||||
|
" elif 'eight'[::ir] in line:\n",
|
||||||
|
" return 8\n",
|
||||||
|
" elif 'nine'[::ir] in line:\n",
|
||||||
|
" return 9\n",
|
||||||
|
" return -1\n",
|
||||||
|
"\n",
|
||||||
|
"line = openafile(\"calibration.txt\")\n",
|
||||||
|
"numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']\n",
|
||||||
|
"Solution = 0\n",
|
||||||
|
"\n",
|
||||||
|
"while True:\n",
|
||||||
|
" c = next(line, None)\n",
|
||||||
|
" if c is None:\n",
|
||||||
|
" break\n",
|
||||||
|
" \n",
|
||||||
|
" front = []\n",
|
||||||
|
" back = []\n",
|
||||||
|
"\n",
|
||||||
|
" for i in c:\n",
|
||||||
|
" if i in numbers:\n",
|
||||||
|
" found_num = lookfornum(c, i, 1)\n",
|
||||||
|
" if found_num > -1:\n",
|
||||||
|
" front.append(str(found_num))\n",
|
||||||
|
" break\n",
|
||||||
|
" front.append(str(i))\n",
|
||||||
|
" break\n",
|
||||||
|
"\n",
|
||||||
|
" for i in c[::-1]:\n",
|
||||||
|
" if i in numbers:\n",
|
||||||
|
" found_num = lookfornum(c[::-1], i, -1)\n",
|
||||||
|
" if found_num > -1:\n",
|
||||||
|
" back.append(str(found_num))\n",
|
||||||
|
" break\n",
|
||||||
|
" front.append(str(i))\n",
|
||||||
|
" break\n",
|
||||||
|
"\n",
|
||||||
|
" front.extend(back[::-1])\n",
|
||||||
|
"\n",
|
||||||
|
" if len(front) > 1:\n",
|
||||||
|
" Solution += int(front[0] + front[-1])\n",
|
||||||
|
" else:\n",
|
||||||
|
" Solution += int(front[0] * 2)\n",
|
||||||
|
"\n",
|
||||||
|
"print(Solution)\n"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
@ -39,15 +39,198 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 54,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
"source": []
|
{
|
||||||
|
"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": {
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
"language_info": {
|
"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,
|
"nbformat": 4,
|
||||||
|
Reference in New Issue
Block a user