Skip to content

Instantly share code, notes, and snippets.

@atusy
Last active February 13, 2021 14:47
Show Gist options
  • Save atusy/8a020c0e7ac7262843fd11b879a98b28 to your computer and use it in GitHub Desktop.
Save atusy/8a020c0e7ac7262843fd11b879a98b28 to your computer and use it in GitHub Desktop.
イニフィニティNumpy 2章Q19(応用):魔王と勇者のデータサイエンス
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"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.9.1-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"(0, 5)\n(0, 100)\n(0, 67)\n(387, 20)\n(920, 8)\n"
]
}
],
"source": [
"import numpy as np\n",
"def battle(num_heros=5, satan_base_power=80, satan_attack_freq=1):\n",
" turns, hps = -1, [np.full(num_heros, 100), 1000000]\n",
" while (num_heros := ((hps := [np.clip(hps[0], 0, 100), hps[1]])[0] > 0).sum(), turns := turns + 1)[0]:\n",
" if (hps := [hps[0] - 20 * (desperate := hps[0] >= 60) + (alive := hps[0] > 0) * (heal := alive & (hps[0] <= 20)).sum(), hps[1] - desperate.sum() * 150 - (~desperate & ~heal & alive).sum() * 100])[1] <= 0:\n",
" break\n",
" for i in (([3, 1] if (satan_attack_freq * turns % 2) else [1, 3]) * (satan_attack_freq // 2 + 1))[:satan_attack_freq]: hps[0][np.where(hps[0] > 0)[0][:i]] -= (satan_base_power if i == 1 else satan_base_power // 2)\n",
" return num_heros, turns\n",
"print(battle())\n",
"print(battle(100, 80, 1))\n",
"print(battle(200, 80, 3))\n",
"print(battle(500, 100, 3))\n",
"print(battle(1000, 200, 5))"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment