# Diamond dozen (Help)

**URL:** <https://discourse.codecombat.com/t/diamond-dozen-help/13792>\
**Category:** Level Help\
**Created:** [February 6, 2018, 4:28pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792 "2018-02-06T16:28:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 4:28pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/1 "2018-02-06T16:28:28Z")

</div>

```python
# Claim the coins while defeating the marauding ogres.
# If you defeat the ogre with the most health, the rest of the ogres will run!
# Coins vanish quickly after appearing, so be sure to find the best value!

def findMostHealth(enemies):
    target = None
    targetHealth = 0
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > targetHealth:
            target = enemy
            targetHealth = enemy.health
        enemyIndex += 1
    return target

# Make a function named findOptimalCoin which returns the coin with the best value.
def findOptimalCoin(coins):
    optimal = -1000
    thione = None
    for coin in coins:
        if coin.value / self.distanceTo(coin) > optimal:
            optimal = coin.value / self.distanceTo(coin)
            thione = coin
    return thione
# Coins rapidly appear and disappear, so pick the best coin.
# Optimize your path by going for the coin with the largest value over distance.
while True:
    enemies = hero.findEnemies()
    enemy = findMostHealth(enemies)
    if enemy and enemy.health > 15:
        while enemy.health > 0:
            hero.attack(enemy)
    else:
        coins = hero.findItems()
        coin = findOptimalCoin(coins)
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)

```

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 4:28pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/2 "2018-02-06T16:28:44Z")

</div>

What is wrong with my code?

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 4:29pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/3 "2018-02-06T16:29:50Z")

</div>

![20%20AM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/5/5d492b7a0e54a15648c7bcc7828053664f86e53e.png)

---

<div class="post-metadata">

**Author:** ![Wealthy-Boss](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wealthy-boss/32/7352_2.png) [@Wealthy-Boss](https://discourse.codecombat.com/u/Wealthy-Boss)\
**Post date:** [February 6, 2018, 7:56pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/4 "2018-02-06T19:56:30Z")

</div>

To Be Honest Their Is Nothing Wrong With Your Code I Read It And Then Used It On The Same Level And It Said Success.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/b/bbd86bc09b739c169e1bdd37ef97604a1bd5153c.png)

---

<div class="post-metadata">

**Author:** ![Hellenar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hellenar/32/9100_2.png) [@Hellenar](https://discourse.codecombat.com/u/Hellenar)\
**Post date:** [February 6, 2018, 8:39pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/5 "2018-02-06T20:39:50Z")

</div>

Then it could be an equipment issue?

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 11:37pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/6 "2018-02-06T23:37:04Z")

</div>

What equipment should I use?

---

<div class="post-metadata">

**Author:** ![TheDemonPrince](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/thedemonprince/32/7366_2.png) [@TheDemonPrince](https://discourse.codecombat.com/u/TheDemonPrince)\
**Post date:** [February 6, 2018, 11:45pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/7 "2018-02-06T23:45:03Z")

</div>

Post a screenshot of what you are wearing, that way we can tell

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 11:54pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/8 "2018-02-06T23:54:41Z")

</div>

Okay I will do that now.

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 11:55pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/9 "2018-02-06T23:55:36Z")

</div>

![11%20PM](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/1/15dc94a0c2aeb3df9f396ed0c2c37d994fa623e5.png)

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 6, 2018, 11:56pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/10 "2018-02-06T23:56:02Z")

</div>

This is it. 🙂

---

<div class="post-metadata">

**Author:** ![Seojin\_Roy\_Lee](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/seojin_roy_lee/32/23539_2.png) [@Seojin\_Roy\_Lee](https://discourse.codecombat.com/u/Seojin_Roy_Lee)\
**Post date:** [February 7, 2018, 12:56am UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/11 "2018-02-07T00:56:17Z")

</div>

you know, this should work. I tried this level with the same equipment and it worked.

---

<div class="post-metadata">

**Author:** ![Wealthy-Boss](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wealthy-boss/32/7352_2.png) [@Wealthy-Boss](https://discourse.codecombat.com/u/Wealthy-Boss)\
**Post date:** [February 7, 2018, 12:05pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/12 "2018-02-07T12:05:57Z")

</div>

Try Using Boots Of Leaping And If That Doesn’t Work Try Contacting @Nick.

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 7, 2018, 10:28pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/13 "2018-02-07T22:28:33Z")

</div>

Gotcha. @Wealthy-Boss

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 12, 2018, 5:18pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/14 "2018-02-12T17:18:22Z")

</div>

Nick won’t respond. 😶

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 12, 2018, 5:23pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/15 "2018-02-12T17:23:50Z")

</div>

@Wealthy-Boss What equipment are you using.

---

<div class="post-metadata">

**Author:** ![Gamestack](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Gamestack](https://discourse.codecombat.com/u/Gamestack)\
**Post date:** [February 12, 2018, 5:35pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/16 "2018-02-12T17:35:41Z")

</div>

Nevermind it was a bug. When i clicked submit it worked.😃

---

<div class="post-metadata">

**Author:** ![thebluemeanie](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@thebluemeanie](https://discourse.codecombat.com/u/thebluemeanie)\
**Post date:** [June 9, 2019, 9:12pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/17 "2019-06-09T21:12:06Z")

</div>

results are slightly different when I run vs when I submit. Submit gets more coins but apparently something is still wrong. Can anyone assist?

```python
// Claim the coins while defeating the marauding ogres.

function findMostHealth(enemies) {
    var target = null;
    var targetHealth = 0;
    var enemyIndex = 0;
    while(enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if(enemy.health > targetHealth) {
            target = enemy;
            targetHealth = enemy.health;
        }
        enemyIndex += 1;
    }
    return target;
}

function valueOverDistance(item) {
    return item.value / hero.distanceTo(item);
}

// Return the item with the highest valueOverDistance(item)
function findBestItem(items) {
    var bestItem = null;
    var bestValue = 0;
    var itemsIndex = 0;
    
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    while(itemsIndex < items.length) {
        var item = items[itemsIndex];
            if (valueOverDistance(item) > bestValue) {
               bestValue = item.value;
                bestItem = item;
        } itemsIndex ++ ;
    }
    return bestItem;
}

while(true) {
    var enemies = hero.findEnemies();
    var enemy = findMostHealth(enemies);
    if(enemy && enemy.health > 15) {
        while(enemy.health > 0) {
            hero.attack(enemy);
        }
    } else {
        var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [June 9, 2019, 10:26pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/18 "2019-06-09T22:26:09Z")

</div>

> [@thebluemeanie](#):
>
> results are slightly different when I run vs when I submit. Submit gets more coins but apparently something is still wrong.

Ah, yes. To clarify, run uses the same seed every run, making it easier to complete it with a specific code that only works for that certain seed. Submit on the other hand, uses a random seed every run, making it impossible to “hard-code” it and succeed. Since I’m horrible at JS, you might want to ask someone like @brooksy125. 😭

---

<div class="post-metadata">

**Author:** ![brooksy125](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brooksy125/32/9966_2.png) [@brooksy125](https://discourse.codecombat.com/u/brooksy125)\
**Post date:** [June 9, 2019, 11:07pm UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/19 "2019-06-09T23:07:34Z")

</div>

I noticed that you are comparing the function `valueOverDistance(item)` to bestValue, but then assigning the item.value to bestValue. Since the item value will be higher than `item.value/ hero.distanceTo(item)`, your first item will always be returned as the best item.

```python
            if (valueOverDistance(item) > bestValue) {  
               bestValue = item.value; //change this value

```

---

<div class="post-metadata">

**Author:** ![thebluemeanie](https://avatars.discourse-cdn.com/v4/letter/t/cc9497/32.png) [@thebluemeanie](https://discourse.codecombat.com/u/thebluemeanie)\
**Post date:** [June 10, 2019, 12:04am UTC](https://discourse.codecombat.com/t/diamond-dozen-help/13792/20 "2019-06-10T00:04:12Z")

</div>

Thanks. Didn’t occur to me that I could use the function again as an object itself. Code reuse I guess.
