Enigmatic Code

Programming Enigma Puzzles

Puzzle #45: Beetles on a clothes line

From New Scientist #3268, 8th February 2020 [link] [link]

Peg beetles are a rare species with rather odd behaviour. As any peg beetle expert will know, these beetles always walk at 1 metre per minute, and when two beetles meet, they immediately reverse direction.

Six peg beetles are on a 2-metre-long clothes line, some walking left to right and others right to left (as the diagram shows). As we join the action, beetle A is at the left-hand end of the line and walking towards the right, while beetle F is at the right-hand end, walking left.

When a beetle reaches the end of the clothes line, it drops off onto the ground.

Which two beetles will be the last to drop off the clothes line, and how long will it be before that happens?

[puzzle#45]

3 responses to “Puzzle #45: Beetles on a clothes line

  1. Jim Randell 9 February 2020 at 9:32 am

    I wrote a program to play out the given scenario. Each tick corresponds to a beetle moving 1cm, so represents a hundredth of a minute.

    Run: [ @repl.it ]

    from enigma import join, printf
    
    # initial positions (all same parity)
    pos = dict(A=0, B=66, C=86, D=116, E=120, F=200)
    
    # initial directions
    inc = dict(A=+1, B=+1, C=+1, D=+1, E=-1, F=-1)
    
    # play out the scenario
    t = 0
    while pos:
      # advance
      t += 1
      d = dict()
      for (k, v) in pos.items():
        v += inc[k]
        if v == 0 or v == 200:
          printf("t={t}: end {k} = {v}")
        else:
          d[k] = v
      # check for collisions
      for x in set(d.values()):
        ks = list(k for (k, v) in d.items() if v == x)
        if len(ks) > 1:
          printf("t={t}: collision {ks} = {x}", ks=join(sorted(ks), sep=","))
          for k in ks:
            inc[k] = -inc[k]
      pos = d
    

    Solution: After 2 minutes beetle B drops of the left hand end and beetle C drops off the right hand end.

    It makes sense that the time it takes for the last two beetles to drop off is 2 minutes, as if you think of the beetle that starts at the extreme left hand end and the beetle that starts at the extreme right hand end both having a baton which is exchanged by beetles when they undergo a collision, then the batons are travelling at a steady rate (the rate of the beetles), in opposite directions, until they reach the far end and drop off with the last beetles, having travelled the entire length of the clothes line.

    And if we think of each beetle as starting with a baton, then the baton continues in it’s original direction until it drops off the end (carried by a beetle). Initially there are 4 beetles heading right, so their batons drop off the right-hand end, carried by the 4 rightmost beetles (F, E, D, C – in the order they drop off). And there are 2 beetles heading left, so their batons drop off the left-hand end, carried by the 2 leftmost beetles (A, B – in the order they drop off). So the two beetles that drop off after 2 minutes are beetle B (at the left hand end) and beetle C (at the right hand end).

    Here’s an animation:

    • Afroz 23 February 2020 at 4:37 pm

      Can you please explain how did you assume the initial position of beetles at 66,86,116,120?

      • Jim Randell 23 February 2020 at 5:33 pm

        Originally I just measured where the beetles were on the diagram and chose values that placed them in approximately the same positions as the diagram.

        But the key thing is that it doesn’t matter what their exact positions are. If you think about the batons travelling from one end of the clothes line to the other, then you see that they travel at a constant speed in a constant direction and fall off with the last two beetles. So as long as there is a beetle at 0 and a beetle at 200 the exact positions of the other beetles doesn’t matter.

        You can try changing the starting positions in the program (but keep all the values multiples of 2 to stop beetles “jumping” over each other), and you’ll see that the final 2 beetles always drop off at t = 200.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: