Code-golfing FizzBuzz

I think you need the trailing semi-colon in professional FizzBuzz competitions.

2 Likes

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
13 Likes

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.

2 Likes

Apparently, 59 is possible!

(x->(t="Fizz"^(x%3<1)*"Buzz"^(x%5<1);t>"" ? t : x)).(1:100) Runs in the REPL

3 Likes

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)

1 Like

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 :person_shrugging:

1 Like

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

3 Likes

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

2 Likes

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 .

5 Likes

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

1 Like

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)))
1 Like

Can shorten this one down to 61
x=1:100;@.println(max(repr(x),"Fizz"^(x%3<1)*"Buzz"^(x%5<1)))

3 Likes

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.

1 Like

some other observations:

  • $x needs max 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 after 100 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])
3 Likes