This commit is contained in:
BasGremmen 2023-12-08 00:16:47 +01:00
parent d6b97b7a58
commit 60a0408db4
1 changed files with 74 additions and 0 deletions

View File

@ -797,6 +797,80 @@
"source": [
"[round(number[0] - number[1]) for number in [[(-1 * numbers[1] + np.sqrt(numbers[0]))/2, ((-1 * numbers[1]) - np.sqrt(numbers[0]))/2] for numbers in [[((numbers[0]**2) - (4 * numbers[1]))]+numbers for numbers in [[int(''.join(re.findall('(\\d+)', line))) for line in open('data/puzzle_6.txt')]]]]][0]"
]
},
{
"cell_type": "markdown",
"id": "4ff4bff7-b7fa-4c44-89f4-cff78d0e7dd3",
"metadata": {},
"source": [
"## Puzzel 7"
]
},
{
"cell_type": "markdown",
"id": "b36323f9-e331-4cb3-b683-0c34e0012466",
"metadata": {},
"source": [
"### Deel 2"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "c12b66ca-a0e7-43e9-a55a-919cd027ab9b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"250665248"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = re.findall(r'(\\w+)\\s(\\d+)',open('data/puzzle_7.txt', 'r').read(), flags=re.MULTILINE)\n",
"values = \"AKQT98765432J\"\n",
"hands = [{'cards': x, 'bid': int(y)} for x,y in data]\n",
"\n",
"fivek = []\n",
"fourk = []\n",
"fh = []\n",
"threek = []\n",
"twop = []\n",
"onep = []\n",
"hc = []\n",
"for hand in hands:\n",
" hand['values'] = np.array([hand['cards'].count(letter) for letter in values if letter != 'J'])\n",
" max_card = np.argmax(hand['values'])\n",
" hand['values'][max_card] += hand['cards'].count('J')\n",
"\n",
" if max(hand['values']) == 5:\n",
" fivek.append(hand)\n",
" elif max(hand['values']) == 4:\n",
" fourk.append(hand)\n",
" elif max(hand['values']) == 3:\n",
" if np.any(hand['values'] == 2):\n",
" fh.append(hand)\n",
" else:\n",
" threek.append(hand)\n",
" elif max(hand['values']) == 2:\n",
" if sum(hand['values'] > 1) == 2:\n",
" twop.append(hand)\n",
" else:\n",
" onep.append(hand)\n",
" else:\n",
" hc.append(hand)\n",
"combos = [fivek, fourk, fh, threek, twop, onep, hc]\n",
"for combo in combos:\n",
" combo.sort(key=lambda hand: ''.join([chr(values.index(letter)+96) for letter in hand['cards']]))\n",
"combos = [val for sublist in combos for val in sublist] \n",
"combos = combos[::-1]\n",
"sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])"
]
}
],
"metadata": {