85
\$\begingroup\$

Write a program or function that, when given a string, filters out as many distinct bytes as it can and returns the cleaned string. However, since your program hates them, none of these bytes can be present in your own code.

Your score will be the number of distinct bytes your program filters out from the input, with the higher the better. This is a max score of 255 (since your program has to be a minimum of one byte). The tiebreaker is the length of your code, with lower being better.

For example, if your program filters out the bytes 0123456789, it receives a score of 10, but your program itself cannot contain these bytes.

Rules

  • Bytes mean octets.
  • You also have the option to take input as a list of integers, with values ranging from 0 to 255. These correspond to the equivalent bytes.
    • Your output should be in the same form as your input
  • No reading your source code
  • Your code must be non-empty
  • Yes, I know there's going to be a Lenguage/Unary answer. But at least golf it please? ;)

Edit Rule:

  • You may choose to ignore a byte, for example if it is indistinguishable from the terminating byte for a string or for EOF. However, this means you can't use it in your submission, nor does it count for your score. If you choose to do so, your max score will be 254, but you don't have to handle that byte being in your input.
\$\endgroup\$
  • 1
    \$\begingroup\$ Is it OK if the function doesn't work if the string contains null bytes (\0)? As C uses null terminated strings, a function cannot know whether it's the end of the string or just a null byte. \$\endgroup\$ – wastl Feb 25 at 15:18
  • 4
    \$\begingroup\$ "Bytes mean octal bytes." Do you mean Octets? Octal is a way of writing numbers, three bits at a time so it doesn't always line up with byte boundaries. \$\endgroup\$ – Kevin Feb 26 at 0:31
  • 1
    \$\begingroup\$ seems to be just begging for an answer in brainfuck \$\endgroup\$ – Jasen Feb 26 at 5:41
  • 1
    \$\begingroup\$ @cmaster For people who cannot see deleted posts: There are a couple of brainfuck attempts, both failing because they cannot distinguish a NUL byte from EOF. \$\endgroup\$ – Ørjan Johansen Mar 5 at 1:11
  • 3
    \$\begingroup\$ @ØrjanJohansen I've added a rule that can let you ignore a byte (for EOF purposes). This shouldn't be too abusable, since it also decreases your max score, and it makes the question more inclusive. \$\endgroup\$ – Jo King Mar 5 at 5:06

33 Answers 33

2
\$\begingroup\$

Java (JDK), score = 240, 129 bytes

e->e.filter(i->i!='&'&i!='e'&i!='-'&i!='>'&i!='.'&i!='f'&i!='i'&i!='l'&i!='t'&i!='r'&i!='('&i!=')'&i!='='&i!='\''&i!='\\'&i!='!')

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This filters out the bytes that are in your code. You are supposed to keep bytes that are in your code from getting filtered. You could just change all the != to ==, gaining a point in the process. \$\endgroup\$ – Embodiment of Ignorance Mar 2 at 15:39
1
\$\begingroup\$

sed, 12 13 bytes

ss[^][^sg]ssg

Try it online!


Previous submission which did not complete the task:

17 bytes without the -E flag: (\| is a metacharacter, | is literal) Try it online.

ss|\|\s\|g\|\\ssg

15 bytes with the -E flag: (| is a metacharacter, \| is literal) Try it online.

ss\||\s|g|\\ssg
\$\endgroup\$
  • \$\begingroup\$ @JoKing I think I read correctly this time. \$\endgroup\$ – GammaFunction Mar 21 at 4:23
1
\$\begingroup\$

dc, 243 score and 243 234 bytes

2i1100100as01110000as11111010as21[]x110000::1[]x110001::1[]x110010::1[]x111010::1[]x111011::1[]x111100::1[]x1011011::1[]x1011101::1[]x1100001::1[]x1101001::1[]x1101100::1[]x1110011::1[]x1111000::[1si]ss[0sil0x;:0<sli0<1sxl2x0<a]l0xsax

Try it online!

This uses the list-of-integers I/O, and presumes that the input list is on the stack. I suppose this may be non-competing because of the nature of traversing a stack; output is essentially reversed. But it was too interesting to pass up. The passing characters are: 2i10as]lx:[;<.

Before showing a slightly ungolfed version, here are the basic ideas. Some characters were going to be unavoidable: digits, [] for macros, lsx for loading, saving, and executing, < for conditional. Starting with 2i puts us in base 10 input, so now we only have to contend with 3 digits. Register and macro names can only be one character long, so if we store the values for all of our included characters in registers, we'll run out of characters to use for registers. An array will solve this and also simplify the lookup, so now we need ;: for arrays as well. dc allows us to convert a code point to a single character string, and macros are just strings. This means any further single-character operations can be recreated using a and saved as macros. We do this for duplicate, print, and z (count stack depth). We also do it for #, the comment character. Executing a macro that's just a comment is akin to a nop, and allows us to avoid spaces. So, we build those commands, store 1s in an array at every code point we allow, and run a macro on the stack that sets a temporary value to 0, checks the stack value against allowed values and sets that earlier value to 1 if so, checks that value and runs a print macro if it's 1, continues until the stack is empty.

2i              #binary time
1100100as0      #macro '0' is duplicate
1110000as1      #macro '1' is print
1111010as2      #macro '2' is stack depth
1[]x            #avoid using whitespace to separate entries by executing an empty macro
    110000::    #insert 1 into element 48, '0'
1[]x110001::    #insert 1 into element 49, '1'
1[]x110010::    #insert 1 into element 50, '2'
1[]x111010::    #insert 1 into element 58, ':'
1[]x111011::    #insert 1 into element 59, ';'
1[]x111100::    #insert 1 into element 60, '<'
1[]x1011011::   #insert 1 into element 91, '['
1[]x1011101::   #insert 1 into element 93, ']'
1[]x1100001::   #insert 1 into element 97, 'a'
1[]x1101001::   #insert 1 into element 105, 'i'
1[]x1101100::   #insert 1 into element 108, 'l'
1[]x1110011::   #insert 1 into element 115, 's'
1[]x1111000::   #insert 1 into element 120, 'x'
[1si]ss         #this macro sets 'i' to '1'
[0si            #reset 'i' to 0
l0x             #duplicate
;:0<s           #load that element of the array, run 's' if >0
li0<1           #load 'i', run '1' (print) if >0
sx              #drop
l2x0<a          #check stack depth (l2x == z), keep running
]l0xsax         #l0xsax is the new dsax

(Golfed off 9 bytes - 100011as] which I was using to make a macro that just contained the comment character # as a nop (to avoid adding spaces or CRs). Forgot at the time I could just run an empty macro []x which is the same number of bytes as loading/executing the # macro.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.