[Community Puzzle] Mirrors

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @stupid36,validated by @Timinator,@Rafarafa and @Pika_equals_Pika.
If you have any issues, feel free to ping them.

This was a fun one.

It took a bit of time to wrap my head around how light that passes through the back of a mirror should be handled, until I noticed the “from any side” remark in the description :slight_smile: Looking at test 6 made it finally click for me.

It might be nice to not have 50% mirrors as the example case though, as it doesn’t matter which direction the light goes for those.

I did not understand the problem, the puzzle description is too brief.

Here’s some starting insight into how the light moves for the example (two mirrors with 50% reflection):

source            m1                 m2                lost
  1 →
            ← 0.5 | 0.5 →
← 0.5             |           ← 0.25 | 0.25 →
          ← 0.125 | 0.125 →          |                 0.25 →
← 0.125           |         ← 0.0625 | 0.0625 →

Then take a look at the test 5 and test 6 scenario’s to understand which portion of light to pass through in each direction, depending on from which direction the beam of light comes from.


I did a simulation of the light, but encountered a timeout during test 4. Can anyone provide advice on what optimizations I can make?

1 Like

Does your simulation trim out small amounts of light?

yes, in test 4 there is 4 mirrors the simulation works if i set the break point for example if light < 0.0001 break, but the result is not accurate whene i do light < EPSILON it runs for longe time

I also ran into timeout issues. My problem was too much focus on the light and not enough focus on the mirrors.

I use Python for my simulation and it works very fast. I use a dictionary, where the keys are (source, destination), and the values are the amounts of light. I initiate a variable “answer” to 0.

I divide my simulation into rounds. In each round, I simulate all the key-value pairs in the dictionary, and store the two results (one reflection and one pass-through) in a new dictionary. Some of the reflection/pass-through results may involve the same (source, destination), and so they are added together. I believe this step makes the data more compact than if I just keep growing the queue. The light which is able to escape back to the initial source is added to the answer. After all the old dictionary items are simulated, I copy the results from the new dictionary to the old one, and begin the next round of simulation. As I trim off results of small values during the process, the dictionary eventually becomes empty and the simulation is all done.

It is irrelevant *when* a certain amount of light passes through a certain mirror.

The reflected and passed amounts remain the same whether you process the light from a single reflected beam or process it together with a beam reflected in the same direction.

Im almost confident Test 4 got the wrong answer. I get 0.9440 instead of 0.9401.
Everything else is correct. Could somebody help me?

1 Like

Please show your calculations, in detail, so that we may have a look at what goes wrong.

def forwards(x, l):
    if l == 0:
    out = l
    back = 0
    global reflection
    while x < n:
        back = out * r[x]
        out = out * (1- r[x])
        round(out, 4)
        round(back, 4)
        if x == 0:
            reflection += back
            t = backwards(x, back)
            reflection += t
        x += 1
        if out < 1e-7:

def backwards(x, l): 
    if l == 0:
        round(l, 4)
        return l
    out = l
    front = 0
    x -= 1
    if out < 1e-7:
        return out
    while x >= 0:
        front = out * r[x]
        out = out * (1- r[x])
        round(front, 4)
        round(out, 4)
        forwards(x + 1, front)
        x -= 1
    return out

added some wierd roundings, so the value might decrease.

I mean calculations, not the code :sweat_smile:

The calculation is happening the same way jeetee is discribing it. What do you mean by showing the calculations? Like post what values are added together? If yes, theres too many number to print out which will make the porgram time out :frowning:

Are you able to show the first few steps of how much light moves from one place to another?

Or are you able to show the total amount of light classified by path (e.g. one total for Mirror 1 to Mirror 2, one total for Mirror 1 to source, etc)?

This one is interesting.
Two points however :

  • for me, “mirrors” have r=1
  • I guess it’s possible to find the formula for the equivalent mirror of two ones. But as I recomputed it, this was clearly not a “simple” one :slight_smile:

I too cannot get test 4 to pass. The closest I get is .9306, and the cutoff point is 0.00000001. The lower I go, the more accurate it gets, but I cannot go any lower than this without it timing out.

I’ve been trying to solve this for 4 days. There has to be an easier way to do this, right?

You may read the earlier discussion above to see if that helps you.