 # Thread: Programming Puzzle 4 - A bug riddled riddle

1. ## Programming Puzzle 4 - A bug riddled riddle

Courtesy: Creative Computing Magazine

A fly and a mosquito start together to circle a building.
The fly circles the building in six minutes, while
the mosquito circles the building in four. If they keep
flying at the same rate, how many minutes will elapse
before the mosquito passes the fly?

Instructions:

Write an xbasic script that answers the question.

Use liberal comments so we can understand what your
script is doing along the way.

If your solution determines the relative positions of
the two bugs at the end of each minute, write
the position information to the trace window at
the end of each minute.

Have fun ! 2. ## Re: Programming Puzzle 4 - A bug riddled riddle

Can't resist.
Code:
```' at 6 minutes, the fly has a half-building lead on the mosquito. that is, the fly has completed one turn, the mosquito has
' completed 1 1/2 turns. the mosquito now has to catch up with the fly. at this point it doesn't matter that the
' course is circular. all that matters is that the fly is now ahead of the mosquito.
' if the fly's speed is x meters/min., then if y=time in mins when the mosquito reaches the fly, the fly will have moved x*y meters.
' but the fly has a head start of 3*x meters.
' the mosquito reaches the fly when
' y*1.5*x = 3*x + x*y
' dividing by x
' 1.5*y=3+y
' .5y = 3
' y = 6 minutes (or 12 minutes from the start)
' now, to program it:
' mosquito's position on circle, each second, is 1/(60*4) of the distance around the circle
' fly's position each second is 1/(60*6) of the way around the circle
circFly=0
circMos=0
for i=1 to 10000 ' each second
circFly=1/360+circFly
if circFly>1 then ' making the second round
circFly=circFly-1
end if
circMos=1/240+circMos
if circMos>1 then 'yep, the mosquito also rounds the building
circMos=circMos-1
end if
if circFly=circMos then
ui_msg_box("Eureka","The mosquito catches the fly at "+i+" seconds.")
exit for
end if
next``` 3. ## Re: Programming Puzzle 4 - A bug riddled riddle

Very interesting! Your solution agrees with mine, but takes a very different approach. I'm not sure I'd ever think that the slower fly was ever "ahead of" the mosquito. I see now that from the perspective of the mosquito that is certainly the case, right from the start, isince the goal is to "lap" the fly.

Your approach could be used by NASCAR to predict how long it might take one car to overtake another.

I'll post my own code after we allow some extra time for others to take a crack at it.

Good work, hope you enjoyed the exercise.

-- tom 4. ## Re: Programming Puzzle 4 - A bug riddled riddle

Oops, mistake. 5. ## Re: Programming Puzzle 4 - A bug riddled riddle

I had similar idea to Peter but made some changes. I was wondering if we could use do..while loop so that we don' t have to do for i= 1 to 10000000000000 or some any number... I could do that in C programming but don't know here. I'm kinda new here... I should give credit to Peter as I used some of his code but added other things to make complete
Code:
```       ' defining all the variables
dim fly as n = 0
dim mosquito as n = 0
dim i as n = 0
dim min as n = 0
fly = 0
mosquito = 0
min = 1
for i=1 to 9999             ' starting a loop to capture the position of both mosquito and fly at each second
fly = (1/360) + fly  ' position of fly after next second
if fly>1 then
fly=fly-1
end if
mosquito = (1/240) + mosquito
if mosquito>1 then
mosquito=mosquito-1
end if
if mod(i,60)=0      ' check if 1 min is passed
? "After "+min+" minute:<br/>"
?"Fly is at: "+ fly * 360 +"degrees from starting position<br/>"
? "Mosquito is at: "+ mosquito * 360 +"degrees from starting position<br/>"
min=min+1
end if
if fly = mosquito then   ' check if their position is same
? "<br/>Finally they meet after "+(i/60)+" minutes"
exit for      ' if the pass then exit loop
end if
next```
Also, I have a question as to how can we get rid of decimal places in calculation. Like (1/3)*3 could be 0.999999 instead of 1.. 6. ## Re: Programming Puzzle 4 - A bug riddled riddle

Thanks, Pratik.

The source of all wisdom, our fearless scorekeeper, "Ignatious A. Pickypicky", offers the following comments & questions for your enlightenment:

a) the Ui_msg_box() function call in line 27 throws an error. It's missing a required parameter.

b) the absence of explanatory comments is noted. Tsk tsk. Pickypicky thinks you can do better. The logic in your For ... Next code block
produces the correct result, but it's not clear why.

c) the "?" symbol is useful when coding in the Interactive Window, but it's not the correct way to write character strings to the trace window. Pickypicky is curious why the question mark is used here as the first character in several lines. You might check the help file for information on the trace.writeln() method. It's far more useful in scripts that will be running from the code page of your Alpha Five control panel.

Thanks for the submittal. Hope you enjoyed the puzzle.

-- tom

ps. I'll be posting my own solution later today. I hope Pickypicky is in a good frame of mind when he checks it out. 7. ## Re: Programming Puzzle 4 - A bug riddled riddle Originally Posted by Tom Cone Jr c) the "?" symbol is useful when coding in the Interactive Window, but it's not the correct way to write character strings to the trace window. Pickypicky is curious why the question mark is used here as the first character in several lines. You might check the help file for information on the trace.writeln() method. It's far more useful in scripts that will be running from the code page of your Alpha Five control panel.
It looks like Pratik has posted code from an A5W page, based on the HTML tags included in the output. If that's the case, ? is the correct way to create the output, and the trace window is not accessible. 8. ## Re: Programming Puzzle 4 - A bug riddled riddle

Thanks, Lenny.

I'll approach the throne with no little trepidation, and gently inform Prof. Pickypicky that he's been spending too much time in desktop land. Pray for me.

-- tom 9. ## Re: Programming Puzzle 4 - A bug riddled riddle

havent read Lenny F. in a while,,, big brother still watching i c :-) 10. ## Re: Programming Puzzle 4 - A bug riddled riddle

Here's my own solution:

Code:
```'Date Created: 05-Jun-2011 03:50:20 PM
'Last Updated: 05-Jun-2011 04:46:14 PM
'Created By  : Tom Cone Jr
'Updated By  : Tom Cone Jr
'
'Puzzle 4 - A bug riddled riddle
'
'Courtesy:  Creative Computing Magazine
'
'A fly and a mosquito start together to circle a building,
'but the fly circles the building in six minutes, while
'the mosquito circles the building in four.  How many
'minutes will elapse before the faster mosquito passes
'the slower fly?

' Each trip around the building traverses a circle.
' Assume traveling in same direction.
' There are 360 degrees in a circle.  On each lap each
'    bug travels through 360 degrees.
' On each trip around the building the position
'    of each "bug" will range from 0 to 360.
' If we subtract 360 for each completed lap we can determine
'    the current location on the 360 degree circle
' The mosquito will pass the fly when their
'     positions on the "circle" (i.e. number of degrees)
'     are the same.

' The int() function truncates the decimal portion of a number
'     and returns the remaining integer.  We'll use it here
'     to subtract 360 on each lap.

Function Posn_Mosquito as N (min as N)
' This is a local function.  Can be located anywhere in the script.

' Compute the current position of the mosquito on a 360 degree circle
' Return the answer as an integer, omitting (truncating) any decimals
'
' (min * 360 / 4) gives us the number of degrees traveled.  i.e.
'     4 min returns 360 degrees
'     8 min returns 720 degrees (twice around the circle)

' int(min / 4) gives us the number of completed laps, i.e.
'     3 min returns zero
'     6 min returns 1

' int(min / 4) * 360 gives us the number of degrees travelled in
'     completed circles.  Knowing this permits us to subtract
'     the degrees in completed laps to find the position in the
'     current lap

Posn_Mosquito = int((min * 360 / 4) - int(min / 4) * 360)

end function

Function Posn_Fly as N (min as N)
' Another local function.  The logic is the same as the previous.
' Formula adjusted because fly takes 6 minutes to travel 360 degrees.
' Function returns the current position of the fly on a 360 degree circle.

Posn_Fly = int((min * 360 / 6) - int(min /6) * 360)

end function

for i = 1 to 24		'arbitary range.  Limits set by experimentation.
' Let i be the number of minutes of flying time.
' Compute the current position of both bugs.  Stop when they are
' at the same place on the circle.

' Display results at end of each minute
trace.writeln("At the end of minute: " + ltrim(str(i))+" Mosquito position: " + ltrim(str(Posn_Mosquito(i)) + " Fly position: " + Posn_Fly(i)))
if Posn_Mosquito(i) = Posn_fly(i) then
exit for	'stop flying.  Mosquito has caught up to the slower fly.
end if
next i

' Display the answer.  Notice the numeric variable "i" is converted to character data type
'   before concatenated with other text.
msgbox("Puzzle 4","It will take " + ltrim(str(i)) + " minutes for the mosquito to pass the fly.")
end``` 11. ## Re: Programming Puzzle 4 - A bug riddled riddle

Your approach could be used by NASCAR to predict how long it might take one car to overtake another
Nascar and many racers do that constantly. Given a known lap time for car a and and given lap time for car b, It is not hard to figure out, especially if you know how many 1000's of a secong car b is behind. BUT It is one thing to catch and another to pass. Remember, I won over 200 short track races. 12. ## Re: Programming Puzzle 4 - A bug riddled riddle

Thanks Lenny for your comment. I did had the code from an A5W page and ui_msg_boz() quite didn't worked so had to use "?". 13. ## Re: Programming Puzzle 4 - A bug riddled riddle

Pratik, we'll be doing our puzzles and exercises using scripts that must run from the code page of the Alpha Five control panel (or from events occurring in a desktop application), not from a web page. I hope you will find them helpful and interesting even though not web based. ok? 14. ## Re: Programming Puzzle 4 - A bug riddled riddle

OK Prof. PickyPicky.. ' My professor who loves to take off points...

Code:
```        fly = 0               ' initialize variables
mosquito = 0
min = 1

for i=1 to 9999                      ' 99 is any arbitrary number (should be at least 720- that's 12 mins )
fly = (1/360) + fly      ' 360 degress in a circle so to check the position after every second
if fly>1 then               ' check if fly completed on circle.
fly=fly-1
end if
mosquito = (1/240) + mosquito   ' same logic as fly (above)
if mosquito>1 then
mosquito=mosquito-1
end if
if mod(i,60)=0           ' check their position after every min
ui_msg_box("After "+min+" minute:","Mosquito and Fly are at "+ltrim(str(mosquito * 360))+" and "+ltrim(str(fly * 360))+" (in degrees)position respectively.")  ' output in a message box
min=min+1
end if
if fly = mosquito then   ' check if their position is same
ui_msg_box("Puzzle 4 solved","Finally they meet after: "+(i/60)+" minutes")
exit for
end if
next```
This is the first time I used in code page in Alpha Five.. Thanks to all your rules and requirements...

Watch out for student Stewie... 15. ## Re: Programming Puzzle 4 - A bug riddled riddle

I've never known a fly or a mosquito to fly anywhere at a consistent rate or straight path - so I swatted them both and therefore have no example to offer.  16. ## Re: Programming Puzzle 4 - A bug riddled riddle

Pratik, very nice!

Robin, you've lost your seat in the front row of the class, and have been exiled to corner in the back of the classroom.  17. ## Re: Programming Puzzle 4 - A bug riddled riddle

Which is no doubt where I belong! 18. ## Re: Programming Puzzle 4 - A bug riddled riddle

Puzzle 4 Attempt.

puz4.txt 19. ## Re: Programming Puzzle 4 - A bug riddled riddle

Very nice! Exceptionally succinct and efficient. Good work. #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•