The Witch's Tower

For nearly a thousand years, Grifelda, in her shrivelled, frog-stained robes, has toiled away in her fortress most forboden, in the pursuit of a potion to grant her powers great enough to earn the awe, terror, and jealousy of every spellcaster in the land. Unfortunately for her, as it goes for all great villains crafting their magnificent doomsday devices... a hero would arrive, at the eleventh hour, to vanquish her, and banish her potion to the chasms.

Grifelda was not without her defenses, though, for at the top of her tower she stood, on the one-hundred and first floor, no less. Below her was a floor with but a single door, and below that, a floor with two doors, and below that, a floor with three... and so on, floor after floor, until the base floor of the tower, with a hundred doors.

Normally, these doors were quite reasonable and predictable in their purposes, but not today, for today was Grifelda's day! Upon the hero's arrival, she cast a great spell, which transformed each and every door into a one-way portal to some other floor, completely unbound by the laws of physics and Euclidean Geometry.

Each door looked exactly the same as every other door, and there was no way to tell to which floor it led without stepping through. She was, however, bound as all villains are bound, not to cheat the hero of his chance at victory. No matter what the hero did, no matter which path he took, she had to leave a way for him to eventually reach the top floor and confront her, lest her defense be dishonorable.

The hero arrives on the bottom floor with one hundred doors just after the spell upon them all is cast. With no distinguishing features to go by, no way to mark the enchanted doors for memory's sake, and no knowledge of the layout of the massive tower, the hero has no choice but to simply enter a single door at random, step onto the floor it opens to, and repeat ad infinitum, until he eventually reaches Grifelda.

Grifelda is, naturally, a perfect mathematician, and so the spell she casts on the doors is optimal, in the sense that it maximizes the expected amount of time it would take for the hero to reach her lair, given that he walks through doors at a constant rate. If D D is the expected number of doors the hero must walk through before reaching Grifelda's lair, what is the decimal digit sum of D \lfloor D \rfloor ?


Details (Clarifications):

  • All of the doors are one-way. This is not to say, however, that there are no pairs of doors which are 'complimentary', in the sense that one door goes from floor X X to floor Y Y , and the other from floor Y Y to floor X X .
  • Doors are permitted to open to the same floor they are on - that is, from floor X X to floor X X .


The answer is 755.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Daniel Ploch
Aug 18, 2014

This problem has a mind-bogglingly enormous solution space, in terms of the number of distinct spells that could be cast by the Witch. The challenge of even counting the number of possible spells she could cast, is, in and of itself, a difficult combinatorics problem! Fortunately, we are searching for spells that have a specific property we wish to maximize, and that turns out to be enough to make the problem tractable.


The difficulty of this problem rises from the fact that Grifelda is an honorable villain - if she weren't, the answer would trivially be \infty , for she could just route all the doors on the bottom floor back to the bottom floor, preventing the hero from ever reaching her lair. So, if we are to maximize the expected time for the hero to reach her lair, we must first get a grasp on the space of spells she might cast, and narrow it down to the optimal one as efficiently as possible.

While the solution actually turns out to be mostly intuitive, with a small little 'gotcha' cherry on top, getting there rigorously is actually a fairly onerous process. I'll present the TL;DR version in the next paragraph with the answer, with the following paragraphs rigorously proving the assertions in the first one.


TL;DR Version

Any optimal spell has the following property:

Choose any ordering { t n } n = 1 101 \{t_n\}_{n=1}^{101} of the floors in the tower, with t 101 t_{101} equal to Grifelda's lair, subject to the constraint D ( t n , t 101 ) D ( t n + 1 , t 101 ) D(t_n, t_{101}) \geq D(t_{n+1}, t_{101}) for all n < 101 n < 101 , where D D is the shortest-path distance function in the graphical sense (where t n t_n are nodes, and doors are directed edges).

It must be the case that, if d n d_n is the number of doors on floor t n t_n , then t n t_n must have ( d n 1 ) (d_n - 1) doors that point directly to t 1 t_1 , and just one door that points to t n + 1 t_{n+1} , except for t 101 t_{101} , which has no doors.

Now, for a given { t n } \{t_n\} , let E ( t n ) E(t_n) be the expected number of doors to be opened by the hero before he reaches t 101 t_{101} , given that he starts on floor t n t_n . Subject to the constraint that d k = 100 d_k = 100 for some fixed k k , E ( t k ) E(t_k) is maximized when d a < d b d_a < d_b for all 1 a < b 100 1 \leq a < b \leq 100 , a , b k a, b \neq k . It can be computed that E ( t k ) E(t_k) is maximal when k = 59 k = 59 , and for that unique configuration, we have:

E ( t 59 ) = 253686955560127297415270748212280220445147578566298142232775185987449253908386439848985416819725524700704346767443420494669394362627027809872591382996377454219 E(t_{59}) = 253686955560127297415270748212280220445147578566298142232775185987449253908386439848985416819725524700704346767443420494669394362627027809872591382996377454219

E ( t 59 ) 2.536869555601273 1 0 158 E(t_{59}) \approx 2.536869555601273 \cdot 10^{158}

And the digit sum of E ( t 59 ) E(t_{59}) is 755 \boxed{755} .


Long Version

Let's first reduce the problem to mathematical terms so that we can rigorously reason about it. Let the floors in the tower be { F k } k = 1 101 \{F_k\}_{k=1}^{101} , where floor F k F_k has precisely k k doors, with the exception of Grifelda's lair, F 101 F_{101} , which has no doors. Let the spell on the doors be arbitrary to start, and let E ( F k ) E(F_k) be the expected number of doors the hero must open to get to Grifelda's lair, given that he is currently on floor F k F_k .


Lemma 1: All floors must be accessible to the hero, and Grifelda's lair should be accessible from all floors.

Let k k such that Grifelda's lair is not ultimately accessible from F k F_k . F k F_k must then not be accessible to the hero from his initial location on F 100 F_{100} , otherwise, upon entering, he would never be able to reach Grifelda's lair, which violates the villain's honor code.

Now take F k F_k , and set all of it's doors to point to Grifelda's lair. Take all doors that previously pointed to Grifelda's lair, and point them at F k F_k instead. This has the effect of making all doors that opened directly to Grifelda's lair in the previous graph now count as 2 2 doors instead of one, which increases E ( F k ) E(F_k) by one for all accessible F k F_k in the previous graph. So the previous graph could not have been optimal. Therefore, any optimal graph must render all floors accessible to the hero, and so Grifelda's lair must be accessible from all floors.


Lemma 2: Every floor should have exactly one door to its topological successor, and all remaining doors should point to the topological root of the tower.

For an arbitrary spell, we can construct an ordering { t m } m = 1 101 \{t_m\}_{m=1}^{101} , consisting of all floors in { F k } \{F_k\} , starting with F 101 F_{101} , and continuing backwards in breadth-first order of the reverse graph of the doors (that is, the directed graph of floors connected by doors, with each door edge reversed), such that every t m t_m , for m < 101 m < 101 , has at least one door opening to a floor t k t_k , with k > m k > m . For floors in the same tier, we can sort them descending with respect to the number of doors in each floor, which renders { t m } \{t_m\} unique for any spell.

Thus, every spell which meets the conditions we have proved thus far necessarily has a unique topological order, and so it follows, that if we can prove that the optimum spell across all spells that satisfy some arbitrary topological order fits the conditions of the stated lemma, then the optimum spell across all spells in total must also satisfy the lemma.

Now, let { t m } \{t_m\} be an arbitrary topological ordering of the floors, and let { d m } \{d_m\} describe the number of doors on each floor correspondingly. We want to maximize E ( t s ) E(t_s) , for the starting position of the hero, t s t_s , and if we can maximize all E ( t m ) E(t_m) while we're at it, great! We first recognize that the value E ( t m ) E(t_m) , for any m m , is representable as the convergent, infinite sum across all finite paths P P starting at t m t_m , of the probability of such a path being taken, times the length of that path in number of doors.

Now, let D D be the set of doors that point to t 101 t_{101} , with D > 1 |D| > 1 . Consider E ( t m ) E(t_m) , represented as the infinite sum we just described, for some arbitrary m m . Then, select some arbitrary d D d \in D , redirect it to some other floor which has ultimate access to at least one x ( D { d } ) x \in (D - \{d\}) (to preserve Lemma 1's results), and compare this new E ( t m ) E(t_m) with the previous one. All paths originally terminating at some x ( D d ) x \in (D - {d}) remain as they are, all paths that terminated at d d are removed.

For each such path P P removed, a swath of new paths are added, which are prefixed by P P , and terminating at some new x ( D d ) x \in (D - {d}) . For each P P removed, the set of new paths added all occur with probabilities that sum to the probability of P P occurring originally, and since each new path is longer than the original P P , the sum of all these new addends necessarily at least matches the original addend for P P , and so the new E ( t m ) E(t_m) is \geq the old E ( t m ) E(t_m) . Since this process can be applied repeatedly so long as D > 1 |D| > 1 , it follows that the optimal spell must have D = 1 |D| = 1 , and so only have one door that points to t 101 t_{101} . It must be the case that t 100 t_{100} houses this door.

Now, assume that, for some k k , 2 k 100 2 \leq k \leq 100 , it is the case that all t m t_m , k m 100 k \leq m \leq 100 , have but one door pointing directly to t m + 1 t_{m+1} , and that there are no other doors pointing to any t m t_m for k < m 100 k < m \leq 100 . Let D D be the set of doors that point to t k t_k , and suppose D > 1 |D| > 1 .

For any E m E_m , it holds that any finite path P P terminating at t 101 t_{101} must, in fact, either be itself a suffix sequence of the doors connecting t m t_m to t m + 1 t_{m+1} for k m 100 k \leq m \leq 100 , or, the path P P must be representable as a finite sequence of paths that terminate at some x D x \in D , eventually terminated by the full sequence of doors that connect those t m t_m to t m + 1 t_{m+1} .

Select some arbitrary d D d \in D , and redirect that door to some other floor which has ultimate access to t k t_k . As with the preceding argument, the change between E ( t m ) E(t_m) for all m m before the change, and after the change, is that all paths P P which contained d d have their addends removed from the sum, and new paths, which contain the redirected d d , are added. We will show this change strictly increases E ( t m ) E(t_m) . Let P q x P_q^x denote the set of all finite paths which start at floor q q , and terminate at floor k k with a pass through door x x , having touched no other door in D D .

Let P q final P_q^{\text{final}} denote the set of paths starting at floor q q which never touch any x D x \in D , and thus terminate in < 101 < 101 steps at t 101 t_{101} . P r ( p ) Pr(p) denotes the probability of path p p being taken exactly, given the hero at the start of it, and p |p| denotes the length of the path, in doors. Then we have the recursive definition:

S ( q , 0 , L ) = p P q final ( P r ( p ) ( p + L ) ) S(q, 0, L) = \sum_{p \in P_q^{\text{final}}} \left( Pr(p) \cdot (|p| + L) \right)

S ( q , r , L ) = x D p P q x ( P r ( p ) S ( k , r 1 , p + L ) ) S(q, r, L) = \sum_{x \in D} \sum_{p \in P_q^x} \left( Pr(p) \cdot S(k, r-1, |p| + L) \right)

And the summation definition:

E ( t m ) = r = 0 S ( m , r , 0 ) E(t_m) = \sum_{r = 0}^{\infty } S(m, r, 0)

Our first argument proved that S ( q , 1 , L ) S(q, 1, L) strictly increases in value when k = 100 k = 100 , for any q q , and by trivial arithmetic, and L L when d d is removed from D D . Because S ( k , 0 , L ) S(k, 0, L) remains constant between the two spells regardless of L L or k k , it follows that S ( q , 1 , L ) S(q, 1, L) increases for all possible q q , k k , and L L , and so, inductively onwards, S ( q , i , L ) S(q, i, L) is strictly increased for the new spell. Since these values sum together to completely define E ( t m ) E(t_m) , E ( t m ) E(t_m) must also be increased for all m m by the redirected door.

The preceding paragraph allows us to apply mathematical induction to claim that the optimal spell, for our given topological order, has exactly one door from each t m t_m to t m + 1 t_{m+1} for all 1 m 100 1 \leq m \leq 100 , and so by process of elimination, all other doors must point back to t 1 t_1 . Given the arguments stated in the first paragraph for Lemma 2, this is sufficient to prove that the optimal spell in general must have this property for its given topological order, and so we prove Lemma 2.


Lemma 3: The optimal spell has a topology that is ascending in terms of the number of doors for all floors, excepting the floor with 100 doors.

First, we'll note that Lemma 2 actually provides us a way to compute E ( t m ) E(t_m) for all m m in a simple, recursive expression. Recall that d m d_m is equal to the number of doors on floor t m t_m . Thus, for all m m , we have the linear equation:

E ( t m ) = 1 + 1 d m E ( t m + 1 ) + d m 1 d m E ( t 1 ) E(t_m) = 1 + \frac{1}{d_m} E(t_{m+1}) + \frac{d_m - 1}{d_m} E(t_1)

Starting from t 1 t_1 , working our way up, and then plugging our way back down, we'll find that a pattern emerges. Specifically:

E ( t m ) = i = 1 100 ( j = i 100 d j ) i = 1 m 1 ( j = i m 1 d j ) E(t_m) = \sum_{i=1}^{100} \left( \prod_{j=i}^{100} d_j \right) - \sum_{i=1}^{m-1} \left( \prod_{j=i}^{m-1} d_j \right)

Grouped another way:

E ( t m ) = i = m 100 ( j = i 100 d j ) + i = 1 m 1 ( j = i 100 d j j = i m 1 d j ) E(t_m) = \sum_{i=m}^{100} \left( \prod_{j=i}^{100} d_j \right) + \sum_{i=1}^{m-1} \left( \prod_{j=i}^{100} d_j - \prod_{j=i}^{m-1} d_j \right)

The second form is quite useful. Suppose, in this sequence, there exist a a and b b such that a > b a > b , but d a < d b d_a < d_b . Observe that if we swap the values d a d_a and d b d_b , the value of E ( t m ) E(t_m) is guaranteed to increase. This is because, in all summands in the two summations, d b d_b is present as a factor if and only if d a d_a is present, and in the cases where a a and b b are on opposite sides of m m , swapping the values decreases the magnitude of the negative addend in the second summand, thus increasing the value of the summand.

Therefore, while in some terms, swapping d a d_a and d b d_b has no effect, in the rest of them, the addend increases in value. So making d m d_m sorted ascending does in fact optimize all E ( t m ) E(t_m) individually.

However, we must be careful here, for while E ( t 100 ) E(t_{100}) is indeed maximal when the sequence { d n } \{d_n\} is fully sorted, this does not necessarily produce the best possible E ( t k ) E(t_k) for all k k where it could be that d k = 100 d_k = 100 . In fact, it can be shown that E ( t m ) > E ( t m + 1 ) E(t_m) > E(t_{m+1}) for all m m on a given spell! We can only conclude that the optimal spell has d k = 100 d_k = 100 for some k k , and for all other n k n \neq k , the sequence { d n k } \{d_{n \neq k}\} is sorted ascending. This concludes Lemma 3.


With Lemmas 1, 2, and 3 under our belt, we are ready to put down the pencil and paper and go to the keyboard instead, as we are, unfortunately, not quite done yet. We still need to figure out which k k in the constraint d k = 100 d_k = 100 yields the maximal E ( t k ) E(t_k) . Using the formula from Lemma 3, we can compute the maximal E ( t k ) E(t_k) for all possible k k , and we'll find that k = 59 k = 59 yields the maximum result. As noted earlier:

E ( t 59 ) = 253686955560127297415270748212280220445147578566298142232775185987449253908386439848985416819725524700704346767443420494669394362627027809872591382996377454219 E(t_{59}) = 253686955560127297415270748212280220445147578566298142232775185987449253908386439848985416819725524700704346767443420494669394362627027809872591382996377454219

E ( t 59 ) 2.536869555601273 1 0 158 E(t_{59}) \approx 2.536869555601273 \cdot 10^{158}

And the digit sum of E ( t 59 ) E(t_{59}) is 755 \boxed{755} .


To put things in perspective, if the hero is able to open a new door every Planck time , and we assume that he is able to proceed in doing so from the big bang, until the projected heat death of the universe in 1 0 100 10^{100} years, the hero would need, on average, 43 million ~43\:\text{million} lifetimes of the universe to reach Grifelda's lair. So maybe this defense isn't so honorable after all.


APPENDIX

After all the analysis, the amount of code needed to get the answer is quite minimal. In it, I used an alternative, recursive representation of E ( t k ) E(t_k) to compute it.

def E(k):
  L = list(range(1,k)) + [100] + list(range(k,100))
  if k == 1:
    return D(L)
  else:
    return D(L) - D(range(1,k))

def D(L):
  if len(L) == 1:
    return L[0]
  else:
    return L[-1] * (D(L[:-1]) + 1)

m = {k: E(k) for k in range(1,101)}
maxE = max(m.values())
maxK = [k for k, v in m.items() if v == maxE]

print maxE * 1.0  # 2.5368695556e+158
print maxK        # [59]
print sum([int(x) for x in str(maxE)])  # 755

Actually, after 1 0 100 10^{100} , there would be no tower, the witch, or the hero (universal thermal equilibrium - Heat Death). Many other reasons apply to why this simply would NOT happen. (and this is with an absurd assumption that the hero operates on planck scales). Anyway, after a certain amount of time, there is no distinction between a mathematical infinity and a psychological infinity; there's a limit to how many possible thoughts a human mind can have .

But if this was some other universe where time went on ad infinitum and the quantity of energy was infinite, and if I were the hero I'd just leave the damn witch to be. Who cares, let her take over the existence; I rather be physically dead than mentally dead.

John M. - 6 years, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...