Shortest Script Challenge: A Hex on FizzBuzz!

Previous challenges listed here.

Today’s challenge is a variant of General FizzBuzz, except instead of “divisible by 3 -> Fizz, divisible by 5 -> Buzz”, uses these mappings:

if evenly divisible by this factor output
2 A
3 B
5 C
7 D
11 E
13 F

And then, since those letters are hexadecimal digits, instead of outputting the ABCDEF string, output the decimal number that it represents in hex. Numbers not divisible by any of the factors should be output as-is.

Some examples:

  • 1 isn’t divisible by any of the factors, so output 1
  • 2 is divisible by 2, so the string would be A, which is the integer 10, so output 10.
  • 21 is divisible by 3 and 7, so the string would be BD, which is the integer 189, so output 189.
  • 30 is divisible by 2, 3, and 5, so the string would be ABC, which is the integer 2748, so output 2748
  • 97 isn’t divisible by any of the factors, so output 97

Write a program to produce the following output (which comes from running this flavor HexBuzz for the numbers from 1 to 100):

1 10 11 10 12 171 13 10 11 172 14 171 15 173 188 10 17 171 19 172 189 174 23 171 12 175 11 173 29 2748 31 10 190 10 205 171 37 10 191 172 41 2749 43 174 188 10 47 171 13 172 11 175 53 171 206 173 11 10 59 2748 61 10 189 10 207 2750 67 10 11 2765 71 171 73 10 188 10 222 2751 79 172 11 10 83 2749 12 10 11 174 89 2748 223 10 11 10 12 171 97 173 190 172 

Reference implementation (293 characters):

$Fizzers = @{2='a'; 3='b'; 5='c'; 7='d'; 11='e'; 13='f'} $Divisors = $Fizzers.Keys | Sort-Object foreach($n in 1..100) { $SuccessfulDivisors = $Divisors.Where{$n % $_ -eq 0} if($SuccessfulDivisors) { [convert]::ToInt32(-join $Fizzers[$SuccessfulDivisors], 16) } else { $n } } 

Can you get to 200? 100? Lower? (Hint: “yes”.)

Some rules:

  1. No extraneous output, e.g. errors, warnings, or text not in the required output
  2. Do not put anything you see or do here into a production script.
  3. Please explode & explain your code so others can learn.
  4. No uninitialized variables.
  5. Hard-coding will be met with stern disapproval.
  6. Script must be no more than ~1000x slower than the reference implementation. (i.e. less than 1 minute of runtime on my machine)
  7. You don’t have to create or convert any hex strings; the only thing that matters is the output.
  8. Enjoy yourselves!

Almost-certain-to-be-out-of-date Leader board:

  1. /u/spyingwind: 145
  2. /u/bis: 293

submitted by /u/bis
[link] [comments]

Leave a Reply