100 days, 100 algorithms
I'm somewhat in awe of this project - that's more algorithms than I could have listed, even with some research, and the discipline of implementing a new one every day for months is impressive.
However, it also shows a systemic weaknesses of programming: understanding the problem domain. I can't say much about the other 99 - some of them are algorithms I've never heard about, and at the very least I would need to make an effort to understand the python code and read it carefully, if not implementing the same thing in Swift, but this one, I spotted immediately:Day 18; MonopolySome time ago a friend of mine asked me to help him with this problem.
Playing Monopoly, what is the probability that you step at position #24 during the first round?
Law of total probability says that the chance to step on certain position is sum of disjoint events of how we could get there. In this case, we get at #24 if we tossed 1 while standing at #23, or we tossed 2 while standing at #22, … or we tossed 6 while standing at #18. This leads to a recursive formula.
Any of my genteel readers who have ever played Monopoly will spot the most obvious problem here: You play Monopoly with two dice, so you can throw 2...12, so this is someone who hasn't done the most fundamental homework.
The less obvious problem is that you're trying to specifically solve _Monopoly_, rather than 'a board with x fields throwing 2D6'. Monopoly has a couple of extra rules: if you throw a double, you get to go again, but if you throw three doubles you go to jail (field 10); once you come out of jail you get another chance to land on field #24; and you have a chance to step on several fields where you may draw a card that moves you to a different field (named or 'three fields back'; IIRC that could even end your round!). In other words, the probability for 'step on field x' is partly determined by the dice, and partly by the game and its very specific rules; if you wanted to give an _precise_ answer, you'd have to calculate by how many routes you can reach each field including the 'go to jail' mechanism which gives all fields after 10 a higher probability and which means that there are ways of reaching #24 from every field between 2 and 35 (double-one, double-one, double 1...6, jail, and try again.) Heck, you could even go to jail several times until you run out of starting money, but if you get _both_ get out of jail free cards....
This also illustrates why board games are not just the sum of straightforward probabilities: once a system becomes complex enough that you cannot simply do the rough calculations in your head, it becomes much more interesting, surprising, and, at a certain level (and given an appropriate mechanic), that rarest thing of all: a creator of narrative. It's no longer 'then I drew card x and rolled y on the dice' but 'so here I was, going about my business curing sick sheep and setting broken limbs when those pesky elves turned up right in front of me and–' (Terry Pratchett's The Witches. Brilliant short game for 2-4 players.)