I think you need the trailing semi-colon in professional FizzBuzz competitions.
got one down to 78 bytesâŚ
println.(1:100 .|>x->(x.%[3,5].<1)|>g->any(g) ? join(["Fizz","Buzz"][g]) : x)
Think this is where the rubber meets the road for me!
I donât know about you folks, but Iâm sticking to 249 bytes:
function fizzbuzz()
for i â 1:100
if i%15==0
println("fizzbuzz")
elseif i%3==0
println("fizz")
elseif i%5==0
println("buzz")
else
println(i)
end
end
end
Seeing that right now⌠Is like being handed a glass of water after being left for dead in the desert. I spent a lot of my freetime code golfing. Donât regret it, but - donât think Iâll be going back anytime soon.
Apparently, 59 is possible!
(x->(t="Fizz"^(x%3<1)*"Buzz"^(x%5<1);t>"" ? t : x)).(1:100)
Runs in the REPL
Iâve been trying to crack this solution but I donât know how to work in a print statement. 59 in the REPL took some work but doable.
Discovered another 59 char/59 byte REPL solution
(x->(s=x%3<1;t=x%5<1;(x,"Fizz"^s*"Buzz"^t)[s|t+1])).(1:100)
best I can do with this idea + print is 65
1:100 .|>i->[i,"Fizz"^(f=i%3<1)*"Buzz"^(b=i%5<1)][f|b+1]|>println
but my personal best is 64 (writeup here)
1:100 .|>i->println((f=i%3<1)|(b=i%5<1) ? "Fizz"^f*"Buzz"^b : i)
the <64 solutions may use a completely different idea(s)âŚsomething with neither ternary operator or array indexing
This may count as a 62 length solution:
(x->max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1))).(1:100).|>println
and 52 on the REPL without .|>println
Can improve to 60 length
1:100 .|>x->max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1))|>println
or
1:100 .|>x->println(max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1)))
Or 51 in the REPL
And yet another improvement:
1:100 .|>printlnâx->max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1))
but it is an improvement in characters (byte-wise there is some unicode extra byte for â
.
clever, I had tried \circ with println and max but then required another pair (,)
Getting closer!
60 bytes/chars
1:100 .|>x->println(max("$x","Fizz"^(x%3<1)*"Buzz"^(x%5<1)))
Edit: sorry havenât seen the post above with this same solution
Is there some hidden Unicode character to replace the max
function call?
Not that I am able to find, interestingly learning a lot about Julia trying to solve this though so its not all for naught
this could be the one if only we could find a way of broadcasting "$x"
without string
for now is 63 bytes/chars
N=1:100;@.println(max(string(N),"Fizz"^(N%3<1)*"Buzz"^(N%5<1)))
Can shorten this one down to 61
x=1:100;@.println(max(repr(x),"Fizz"^(x%3<1)*"Buzz"^(x%5<1)))
Some remarks/observations
My suspicion is "$x"
is used.
Defining the exponents for the strings outside the primary call saves parentheses but then adds characters to insert: i.e. f=x%3<1
is the same length as (x%3<1)
but then need to use;
.
Using the ternary operator a ? b : c
certainly takes up too much space
Trying to put it into one string doesnât work because need to concatenate $x
with FizzBuzz
and there is no efficient way to then call exactly which indices are wanted, would have been nice given strings behave as arrays
Would be nice if show()
works but it much like print
displays on a single line. dump()
was an option I tried, it prints on new lines but also includes type of the item being printed.
I have found no alternative to using max()
I had wondered if there was a clever way to use 0:99
but canât seem to find a workaround for this.
some other observations:
-
$x
needsmax
and we probably already have the best solution with that idea -
0:99
is appealing but I donât see a way of using it neither. This could be a false friend though, as weâre looking to remove just one byte/char and that blank space after100
is the obvious target - broadcasting over
N=1:100
usually yields the best solution, but in this case weâre stuck at 61 - another sometimes winning approach is to write a recursive solution, but I cannot do better than 68
i<n=102>i+1<println((f=1>i%3)|(b=1>i%5) ? "Fizz"^f*"Buzz"^b : i);1<0
- we can use
gcd
for a single check instead of two (66)
f="Fizz";b="Buzz";1:100 .|>i->println([i i f f*b b][gcd(i,15)%11])