In the wake of the many (two?) FizzBuzz-related challenges posted recently on PPCG, I've been tempted to come up with my own. Behold...
Fizz Buzz Lightyear
Write a program or function that takes an integer
n and prints out
FizzBuzz for any number divisible by 15,
Fizz for any number divisible by 3, and
Buzz for any number divisible by 5, up to (and including)
n. Output for all
i must be followed by a newline. But there's a twist!
For every third time you print
Buzz, Buzz Lightyear finally heeds your call and crash lands in your program. He then introduces himself - but since he crash landed, some of what he said gets mixed up with your program's output:
Buzz Lightyear, Space Ranger, Universe Protection Unit. FizzBuzz Lightyear, Space Ranger, Universe Protection Unit.
(that is, only append
Lightyear, Space Ranger, Universe Protection Unit. to
FizzBuzz - whatever it is you'd have displayed otherwise. Note the leading space)
However, Buzz Lightyear, being the Space Ranger he is, has very acute hearing, and so printing
FizzBuzz will count towards your
Then, Buzz hangs around to defend your computer from all of that evil output, until you hit another number that's divisible by 5 (or 15, since those are divisible by 5, too). What that means is until you have to print
FizzBuzz) again, you don't print anything at all.
When you finally reach that condition, Buzz departs:
To infinity and beyond!
This is the expected output for
n = 25: (notice how it skips 16 through 19)
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz Lightyear, Space Ranger, Universe Protection Unit. To infinity and beyond! Fizz 22 23 Fizz Buzz
Optional trailing newline is acceptable.
This is code golf; as such, shortest code, in bytes, wins.
n is valid and greater than or equal to 15 (which is when the challenge first deviates from standard fizzbuzz)
Buzz greets you when the "Buzz counter" (which counts both
FizzBuzz) hits 3; he departs when the next
Buzz (including, again, both
FizzBuzz) is printed.
The number in which he departs does not count towards the next "Buzz counter"; you should instead start counting the
Buzzes again from 0. For instance, a program running with
n = 25 (example output) should end with a "Buzz counter" of 1, since that's how many times
Buzz was printed since the last time he departed.
n falls between one of Buzz's arrivals and one of his departures (i.e., he's still there - you're not printing anything), graceful termination is expected. Therefore, the last line of output would be his introduction
Standard loopholes are forbidden.
nfalls in-between Buzz Lightyear's arrival and his departure? \$\endgroup\$