Solution in javascript. I don't think this is necessarily the correct way to do this, just one way. I wanted to figure out an efficient way to use the data structures that are available in Perl to solve this. Setting up the data is really what I want to learn

// Day 21: RPG Simulator 20XX // boss's stats // Hit Points: 100 // Damage: 8 // Armor: 2 function part1() { const WEAPONS = new Map([ ["dagger", { cost: 8, damage: 4, armor: 0 }], ["shortsword", { cost: 10, damage: 5, armor: 0 }], ["warhammer", { cost: 25, damage: 6, armor: 0 }], ["longsword", { cost: 40, damage: 7, armor: 0 }], ["greataxe", { cost: 74, damage: 8, armor: 0 }], ]); const ARMOR = new Map([ ["nothing", { cost: 0, damage: 0, armor: 0 }], ["leather", { cost: 13, damage: 0, armor: 1 }], ["chainmail", { cost: 31, damage: 0, armor: 2 }], ["splintmail", { cost: 53, damage: 0, armor: 3 }], ["bandedmail", { cost: 75, damage: 0, armor: 4 }], ["platemail", { cost: 102, damage: 0, armor: 5 }], ]); const RINGS = new Map([ ["nothing", { cost: 0, damage: 0, armor: 0 }], ["damage+1", { cost: 25, damage: 1, armor: 0 }], ["damage+2", { cost: 50, damage: 2, armor: 0 }], ["damage+3", { cost: 100, damage: 3, armor: 0 }], ["defense+1", { cost: 20, damage: 0, armor: 1 }], ["defense+2", { cost: 40, damage: 0, armor: 2 }], ["defense+3", { cost: 80, damage: 0, armor: 3 }], ]); const BOSS = new Map([ ["damage", 8], ["armor", 2], ["health", 100], ]); const PLAYER = new Map([ ["damage", 0], ["armor", 0], ["health", 100], ]); const getTotalStats = (weapon, armor, leftRing, rightRing) => { return { cost: weapon.cost + armor.cost + leftRing.cost + rightRing.cost, damage: weapon.damage + armor.damage + leftRing.damage + rightRi +ng.damage, armor: weapon.armor + armor.armor + leftRing.armor + rightRing.a +rmor, }; }; const hitPerSecond = (defenderHealth, defenderArmor, attackerDmg) => Math.ceil(defenderHealth / Math.max(1, attackerDmg - defenderArmor +)); const makeMove = (boss, player) => hitPerSecond(boss.get("health"), boss.get("armor"), player.get("da +mage")) <= hitPerSecond(player.get("health"), player.get("armor"), boss.get(" +damage")); function* possibleBundles() { for (let weapon of WEAPONS.values()) { for (let armor of ARMOR.values()) { for (let leftRing of RINGS.values()) { for (let rightRing of RINGS.values()) { if (rightRing.cost !== leftRing.cost) yield getTotalStats(weapon, armor, leftRing, rightRing); } } } } } let result = Infinity; for (let bundle of possibleBundles()) { PLAYER.set("damage", bundle.damage).set("armor", bundle.armor); if (makeMove(BOSS, PLAYER)) result = Math.min(result, bundle.cost) +; } console.log(result); } part1();

In reply to Re^2: using ref to hash of hash effectively by kalee
in thread using ref to hash of hash effectively by kalee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.