# PureBasic Forum

 It is currently Fri May 24, 2013 4:00 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 11 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: New Game... Puzzle #1Posted: Fri Mar 16, 2012 2:05 pm
 Enthusiast

Joined: Sat Apr 26, 2003 2:15 pm
Posts: 353
Location: Vancouver Island, Canada
I dropped in on a database forum and they were having a puzzle solving contest, and it looked like a lot of fun, so I thought I’d start something here.

Hopefully, these puzzles will give us a springboard from which we can discuss, teach, learn, and benefit from each other's experience.
It appears that the idea is to show new users how to code using PureBasic.
The puzzles need not be difficult to solve, but show loops, methods, etc.

Thanks to: "Creative Computing Magazine" for this puzzle.
Hopefully we can keep this going with more puzzles showing Pure coding.

Puzzle #1

Instructions:
a) Your solution must use the statement: EnableExplicit
b) Your code needs to be well commented.
c) Use MessageRequester to display your results.

-------------------------------------------------
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?
--------------------------------------------------

Let's see some solutions....

_________________
- It was too lonely at the top.

Top

 Post subject: Re: New Game... Puzzle #1Posted: Fri Mar 16, 2012 7:29 pm

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1443
Location: Germany
Hi,

I hope my solution is right:
Code:
EnableExplicit

#FlyTime = 6          ; 6 minutes per lap
#MosquitoTime = 4     ; 4 minutes per lap

; definition of the needed variables
Define.f FlyRound, MosquitoRound
Define.i Minutes

; calculation loop
Repeat
FlyRound + 1 / #FlyTime             ; per minute the fly can fly 1/6 round
MosquitoRound + 1 / #MosquitoTime   ; per minute the mosquito can fly 1/4 round
Minutes + 1                         ; next minute
Until MosquitoRound >= FlyRound + 1  ; the mosquito needs minimum 1 full lap more

; show the calculated time
MessageRequester("Result", "It needs " + Str(Minutes) + " minutes until the Mosquito laps the Fly")
Bernd

P.S.: A more accurate version:
Code:
EnableExplicit

#FlyTime = 6          ; 6 minutes per lap
#MosquitoTime = 4     ; 4 minutes per lap

; definition of the needed variables
Define.f FlyRound, MosquitoRound
Define.i Seconds

; calculation loop
Repeat
FlyRound + 1 / #FlyTime / 60.0
MosquitoRound + 1 / #MosquitoTime / 60.0
Seconds + 1                         ; next second
Until MosquitoRound >= FlyRound + 1  ; the mosquito needs minimum 1 full lap more
Seconds - 1

; show the calculated time
MessageRequester("Result", "It needs " + FormatDate("%ii:%ss", Seconds) + " minutes Until the Mosquito laps the Fly")
If you set the Mosquito to 1 Round per minute it needs 1:12 minutes.

Last edited by infratec on Fri Mar 16, 2012 7:59 pm, edited 1 time in total.

Top

 Post subject: Re: New Game... Puzzle #1Posted: Fri Mar 16, 2012 7:34 pm

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
Code:
EnableExplicit
Define.f flySpeed = 1 / 6, mosquitoSpeed = 1 / 4   ;speeds are in laps/minute

Define solution.s
If mosquitoSpeed = flySpeed
solution = "The Mosquito never passes the Fly because they are flying at the same speed."
ElseIf mosquitoSpeed < flySpeed
solution = "The Mosquito never passes The Fly because it is slower."
Else
solution = "The Mosquito passes the Fly after " + RTrim(RTrim(StrF(-1 / (flySpeed - mosquitoSpeed), 2), "0"), ".") + " minutes."
EndIf

MessageRequester("Solution", solution)

@Edit: Corrected formula for answer.

_________________

Last edited by Demivec on Fri Mar 16, 2012 11:32 pm, edited 2 times in total.

Top

 Post subject: Re: New Game... Puzzle #1Posted: Fri Mar 16, 2012 8:19 pm

Joined: Thu Jun 24, 2004 2:44 pm
Posts: 4715
Location: Berlin - Germany
I think, some people have many time, so here the best simulation
Code:
; based on example by infratec
; added simulation of minute

EnableExplicit

#FlyTime = 6          ; 6 minutes per lap
#MosquitoTime = 4     ; 4 minutes per lap

; definition of the needed variables
Define.f FlyRound, MosquitoRound
Define.i Minutes

; calculation loop
Repeat
FlyRound + 1 / #FlyTime             ; per minute the fly can fly 1/6 round
MosquitoRound + 1 / #MosquitoTime   ; per minute the mosquito can fly 1/4 round
Minutes + 1                         ; next minute
Delay(1000 * 60)                    ; simulate the minute  ;-)
Until MosquitoRound >= FlyRound + 1  ; the mosquito needs minimum 1 full lap more

; show the calculated time
MessageRequester("Result", "It needs " + Str(Minutes) + " minutes until the Mosquito laps the Fly")

_________________
PureBasic 5.11 | Windows 7 SP1 (x64) | Mageia 3 (x64) | RealSource

The use of EnableExplicit is free of charge and avoids errors.

Top

 Post subject: Re: New Game... Puzzle #1Posted: Fri Mar 16, 2012 8:52 pm
 Enthusiast

Joined: Sat Apr 26, 2003 2:15 pm
Posts: 353
Location: Vancouver Island, Canada
Quote:
ts-soft ....I think, some people have many time, so here the best simulation

That's the whole idea.. keep them coming!

_________________
- It was too lonely at the top.

Top

 Post subject: Re: New Game... Puzzle #1Posted: Fri Mar 16, 2012 8:56 pm

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 2240
Location: Italy
12 minutes ?

Code:
; Instructions:
; a) Your solution must use the statement: EnableExplicit
; b) Your code needs to be well commented.
; c) Use MessageRequester to display your results.

; 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?

EnableExplicit

Define FlySpacePerSecond.d = 1.0 / (6.0 * 60) ; the speed in laps for second for the Fly
Define MosquitoSpacePerSecond.d = 1.0 / (4.0 * 60) ; the speed in laps for second for the Mosquito

Define iTimeSec = 1 ; the current instant in time (expressed in seconds)
Define FlyPos.d ; the laps (+ fractional part) flown by the Fly
Define MosquitoPos.d ; the laps (+ fractional part) flown by the Mosquito

Repeat
FlyPos = iTimeSec * FlySpacePerSecond ; calc the Fly position
MosquitoPos = iTimeSec * MosquitoSpacePerSecond ; calc the Mosquito position

Debug "Time " + Str(iTimeSec )
Debug "Fly = " + StrD(FlyPos)
Debug "Mosquito = " + StrD(MosquitoPos)

If MosquitoPos >= FlyPos + 1  ; must have done a lap more
MessageRequester("GOTCHA", "The Mosquito reached againg the Fly after " + Str(iTimeSec) + " seconds.")
Break
EndIf

iTimeSec + 1 ; the current instant in time
ForEver

_________________
[ Home ] [ My PC ] [ New to PB ? ]

Top

 Post subject: Re: New Game... Puzzle #1Posted: Fri Mar 16, 2012 9:52 pm

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1443
Location: Germany
the mathematical approach:
Code:
; x = 1/6 * t
; x = 1/4 * t - 1

; 1/6 * t = 1/4 * t - 1

; (1/6 - 1/4)t = -1

; t = -1 / (1/6 - 1/4)

MessageRequester("Result", "It needs " + StrF(-1.0 / (1/6.0 - 1/4.0), 0) + " minutes until the Mosquito laps the Fly")
Bernd

P.S.: I think that's the shortest solution

Top

 Post subject: Re: New Game... Puzzle #1Posted: Sat Mar 17, 2012 9:20 am
 Enthusiast

Joined: Sat Apr 26, 2003 2:15 pm
Posts: 353
Location: Vancouver Island, Canada
Yes... that's the shortest.

Say. You guys are good!

Mutters to self: (I'll have to look for something a little harder)

If anyone has any other programming puzzles... please jump in.

--Bob

_________________
- It was too lonely at the top.

Top

 Post subject: Re: New Game... Puzzle #1Posted: Sat Mar 17, 2012 12:45 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1575
Location: Berlin, Germany
blueb wrote:
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?

infratec wrote:
I think that's the shortest solution

Here is an even shorter mathematical solution:
Code:
; Prime factorization:
; 6 = 2*3
; 4 = 2*2
; => Least_Common_Multiple(6,4) = 2*2*3
MessageRequester("Result", "It will take " + Str(2*2*3) + " minutes.")

Regards, Little John

PS: Calculating the Least Common Multiple with PureBasic is simple, but not sooo short anymore.

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].

Top

 Post subject: Re: New Game... Puzzle #1Posted: Sat Mar 17, 2012 12:59 pm

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1443
Location: Germany
Hi Little John,

yes it 's shorter, but...

I think the solution is wrong.

What happens if the mosquito needs only 2 minutes ?

With my code it results in 3 minutes, which is correct in my opinion.
(Mosquito 1.5 rounds and fly 0.5 rounds)

When I use your calculation:
Code:
; Prime factorization:
; 6 = 2*3
; 2 = 2*1
; => Least_Common_Multiple(6, 2) = 2*1*3
MessageRequester("Result", "It will take " + Str(2*1*3) + " minutes.")
It results in 6 minutes.

Bernd

Top

 Post subject: Re: New Game... Puzzle #1Posted: Sat Mar 17, 2012 1:09 pm

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1575
Location: Berlin, Germany
Hi Bernd,

you are right!

My solution answered the puzzle "How many minutes will elapse before the mosquito and the fly meet at the same point where they started?"
But that wasn't the question here.

Regards, Little John

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 11 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 8 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite