Advent of Code: Day 22
Wizard Simulator 20XX

First puzzle

Day 21 puzzle was just to warm up; here we are going to fight with magic. The combat is very similar in the sense that the player goes first, and they the player and the boss takes turns in to attack each other. In this case, the players attacks are with magic casting spells, and the boss continues to attack as in the previous day.

The player starts with 500 mana, and each spell cost a certain amount of it. If the player doesn’t have enough mana to cast a spell, it loses. Here are the list of spells:

The effect rules:

It is critical to understand the fight examples on the puzzle description. They don’t show all the possible scenarios, but it gives us a good idea of the situations. I won’t re-write them here because they are very long.

The solution of the puzzle is to find the least amount of mana we need to win the fight taking into account that the “recharged” mana is not negative mana. The total mana spent is the sum of all the mana spent on casting spells. The player has 50 hit points and 500 mana to start. The boss hit points, and the damage score is the puzzle’s input.

I tried the same idea of calculating all the possible games as it was done on Day 21 puzzle. It didn’t work because there are too many possible scenarios. Again, after a few days and a few hours per day trying to find a solution, I gave up and find help in Google. This is the second time translate a solution to Scala from Python, and the original Python (code)[https://github.com/ChrisPenner/Advent-Of-Code-Polyglot/tree/master/python/22] is fast and easy to understand.

The input parsing here is just the representation of the spells as a case class:

In the end, I think the Python version is easier to understand. On my Scala version, I tried as much as possible to use immutable data structure and to pass the behavior as functions, but I think it didn’t add simplicity:

Second puzzle

The second part of the puzzle add another rule: At the start of each player turn, the player lose one hit point before any effect apply. In the case of our modeling, we just need to change the behavior the player and create a new game generator function:

You can find this code along with my input and puzzle answers at here.

*****
Written by Darien Martinez on 22 March 2016