From 60a0408db4642bf03f99df04e382d704c5792a82 Mon Sep 17 00:00:00 2001 From: BasGremmen Date: Fri, 8 Dec 2023 00:16:47 +0100 Subject: [PATCH] Yee --- Book_1.ipynb | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/Book_1.ipynb b/Book_1.ipynb index bad31ee..ac460b1 100644 --- a/Book_1.ipynb +++ b/Book_1.ipynb @@ -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": {