Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q54\n", "The file, [q54_data.txt](/notebook/raw/q54_data.txt), contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1’s cards and the last five are Player 2’s cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player’s hand is in no specific order, and in each hand there is a clear winner.\n", "\n", "How many hands does Player 1 win?" ] }, { "cell_type": "code", "execution_count": 1, "id": "62ce1f5d-9a9e-4fca-9713-10e7c3085bbd", "metadata": {}, "outputs": [], "source": [ "hands_data = open(\"q54_data.txt\",\"r\").read()" ] }, { "cell_type": "code", "execution_count": 2, "id": "bd7ddc2f-5964-49c3-a927-774399b0b0a9", "metadata": {}, "outputs": [], "source": [ "def hands_string_to_list(hands_string):\n", " return [row.strip().split(\" \") for row in hands_string.split(\"\\n\")]\n", "\n", "def hand_rank(hand):\n", " values = {r:i for i,r in enumerate('23456789TJQKA', 2)}\n", " straights = [(v, v-1, v-2, v-3, v-4) for v in range(14, 5, -1)] + [(14, 5, 4, 3, 2)]\n", " ranks = [(1,1,1,1,1),(2,1,1,1),(2,2,1),(3,1,1),(),(),(3,2),(4,1),()]\n", " hands_count = {}\n", " for card in hand:\n", " card_value = card[0]\n", " hands_count[card_value] = hands_count.get(card_value,0)+1\n", " score = list(zip(*sorted([(v, values[k]) for k,v in hands_count.items()],reverse= True)))\n", " score[0] = ranks.index(score[0])\n", " if len(set(card[1] for card in hand)) == 1:\n", " # Flush\n", " score[0] = 5 \n", " if score[1] in straights:\n", " if score[0] == 5:\n", " # Straight Flush\n", " score[0] = 8 \n", " else:\n", " # Straight\n", " score[0] = 4 \n", " return score\n", "\n", "def num_player_one_win(hands_string):\n", " return sum(hand_rank(hand[:5]) >= hand_rank(hand[5:]) for hand in hands_string_to_list(hands_string))\n", " " ] }, { "cell_type": "code", "execution_count": 3, "id": "3e78fa42-856e-4722-a279-245d9b8516a5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 8.02 ms, sys: 0 ns, total: 8.02 ms\n", "Wall time: 7.86 ms\n" ] }, { "data": { "text/plain": [ "376" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "num_player_one_win(hands_data)" ] }, { "cell_type": "code", "execution_count": null, "id": "4dd1e332-2bdf-403f-8157-9c4131421bec", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q53
Next Notebook:
Project Euler Q55
Loading