With the AMPscript challenge now closed – it’s time to reveal the winners and share their amazing solutions with the community. If you need a refresher on the competition, the details are here: https://www.cameronrobert.com.au/sfmc/ampscript-fizzbuzz-challenge/
It has to be said that the quality of submissions from the community has been amazing! 28 people took the time to solve the challenge and most of the submissions followed the same basic (and efficient) approach to the problem. I’m really looking forward to running more of these types of activities for the community in the future!
So lets meet our code champions and their solutions!
1st Place – Shams Saif (183)
%%[for @i=1 to 1000 do SET @a=Concat(iif(Mod(@i,3)<1,'Fizz',n),iif(Mod(@i,5)<1,'Buzz',n),iif(Mod(@i,7)<1,'Boing',n),iif(Mod(@i,11)<1,'Bang',n))]%%%%=iif(@a=='',@i,@a)=%%<br>%%[next]%%
✔️Removed unnecessary spaces – saving multiple characters
✔️Minified all variables – saving multiple characters
✔️Dropped the unnecessary @i from the next function – saving 3 characters
✔️Used <1 rather than ==0 in MOD() functions – saving 4 characters
✔️Used n rather than ” in the iif() statement – saving 4 characters
Shams managed to find every possible string shortening trick to bring his code down to an incredible 183 characters!
“I discovered that the iif() statement accepts a random character in the false clause by chance. I was trying to find a way to get rid of the ” characters and found than any single character works! You have to put a character though, a space does not work.”
Shams Saif
Fun Fact: Only 2 people submitted the single character iif() trick. Shams Saif and Jarrett Bush, and it was discovered that you can use any single character (not just n), however this trick doesn’t work in Email Studio (only on CloudPages).
2nd Place – Sascha Huwald (187)
%%[FOR @i=1 TO 1000 DO set @s=Concat(IIF(MOD(@i,3)<1,'Fizz',''),IIF(MOD(@i,5)<1,'Buzz',''),IIF(MOD(@i,7)<1,'Boing',''),IIF(MOD(@i,11)<1,'Bang',''))]%%%%=IIF(@s=='',@i,@s)=%%<br>%%[NEXT]%%
✔️Removed unnecessary spaces – saving multiple characters
✔️Minified all variables – saving multiple characters
✔️Dropped the unnecessary @i from the next function – saving 3 characters
✔️Used <1 rather than ==0 in MOD() functions – saving 4 characters
❌Used n rather than ” in the iif() statement
With just 4 characters difference caused by the single character iif() trick, Sascha’s code is an excellent response to the FizzBuzzBoingBang challenge.
I encourage everyone to solve the problem before you start coding. A conceptual design of solving a problem can be applied to any programming language. The key is to solve the problem first.
Sascha Huwald
Sascha Huwald also identified that using a Code Resource (Text) Cloud Page eliminates the need for the 4 characters used for line break “<br>”. This fact would make the shortest possible solution to this problem only 180 characters! Nice find Sascha!
Sascha’s AMPscript code game is strong, but his SSJS code game is a force to be reckoned with! For those who didn’t know – Sascha is the Author of Email360 and the SSJS-lib; a sleek and powerful library for faster and easier development in Salesforce Marketing Cloud.
3rd Place – Josephine Danielle Biscocho (192)
%%[FOR @i=1 TO 1000 DO SET @j=concat(iif(mod(@i,3)>0,"","Fizz"),iif(mod(@i,5)>0,"","Buzz"),iif(mod(@i,7)>0,"","Boing"),iif(mod(@i,11)>0,"","Bang"))output(iif(empty(@j),@i,@j))]%%<br>%%[NEXT]%%
✔️Removed unnecessary spaces – saving multiple characters
✔️Minified all variables – saving multiple characters
✔️Dropped the unnecessary @i from the next function – saving 3 characters
✔️Used >0 rather than ==0 in MOD() functions – saving 4 characters
❌Used n rather than ” in the iif() statement
❌Used empty(@j) rather than @j==” in the output iif() statement – adding 3 characters
❌Used output() rather than iif() as an inline function – adding 2 characters
Josephine Danielle Biscocho used a slightly different approach that involved leveraging the Output() function. While this method did cost her 2 additional characters, she still had some of the cleanest code submitted by removing all unnecessary spaces and using the >0 solution in the MOD() function; a trick that only 7 people used!
Honourable Mentions
The solutions below didn’t place in the top 3 for the criteria of the competition, however their authors did bring something new and interesting to the challenge that the community will benefit from reading.
Note: I’ve formatted these solutions slightly to make them easier to read and understand their unique solution!
Rafał Wolsztyniak – BuildRowsetFromString & Replace
%%[SET @r=BuildRowsetFromString('3♫5♫7♫11♫Fizz♫Buzz♫Boing♫Bang','♫')
FOR @x=1 to 1000 DO
SET @l=@x
FOR @y=1 to 4 DO
SET @l=Concat(@l,IIF(Mod(@x,Field(Row(@r,@y),1))==0,Field(Row(@r,Add(4,@y)),1),''))
NEXT @y]%%
%%=v(IIF(@l==@x,@l,Replace(@l,@x,'')))=%%<br>
%%[NEXT]%%
Rafał’s solution involved the use of a RowSet & Replace function, but don’t like the ♫ fool you – this solution is one of the most scalable options submitted.
Marouenne Belhaj – 2x BuildRowsetFromString
%%[SET @m=BuildRowsetFromString("3*5*7*11","*")
SET @p=BuildRowsetFromString("Fizz*Buzz*Boing*Bang","*")
FOR @i=1 to 1000 DO
SET @c=""
FOR @j=1 TO 4 DO
SET @c=Concat(@c,IIF(MOD(@i,FormatNumber(Field(Row(@m,@j),1),"N"))==0,Field(Row(@p,@j),1),""))
NEXT
SET @c=IIF(@c=="",@i,@c)
]%%
%%=v(@c)=%%<br>
%%[
NEXT
]%%
Similarly to Rafał’s solution, Marouenne has developed an extremely scalable solution that can be very easily adapted to add new FizzBuzz conditions. An excellent example of code that is built to be easy to read and altered in the future!
Jarrett Bush – 183 character MOD() comparison trick
%%[FOR @x=2 TO 1001 DO
SET @y=Concat(IIF(Mod(@x,3),"Fizz",?),IIF(Mod(@x,5),"Buzz",?),IIF(Mod(@x,7),"Boing",?),IIF(Mod(@x,11),"Bang",?))]%%%%=IIF(@y=="",Add(@x,-1),@y)=%%<br>%%[NEXT]%%
Jarrett found this little hack while playing with the MOD function. The MOD function give a remainder as the result, so if the remainder is “1”, that also works as a TRUE value for a comparison. Therefore you can drop the comparison statement and force a reminder of 1 by shifting the counter up by 1 (from 2 to 1001). Amazing find Jarrett!