A language that is very useful in restricted source and other such challenges is Unary, a brainfuck derivative in which programs are written in with only one character. Your job is to write a program to convert programs from brainfuck to unary and a program to do the opposite, both programs in the same language. Your score will be the sum of the lengths of the two programs.

How do you convert from brainfuck to unary?

  • First convert you brainfuck code to binary according to this table:

Conversion Table

  • Now concatenate the code into one giant binary number in order of the code.
  • Prepend a 1 to the string to ensure a unique binary number.
  • Convert from a binary number to a unary number using any character.
  • Ex: +. would be 000000000000000000000000000000000000000000000000000000000000000000000000000000000000 (84 zeroes).

Brainfuck -> Unary Specs

  • Since the resulting programs will be impossibly huge, print not the actual program but merely the length of the resulting program.
  • Take the brainfuck program as a string through stdin, function arg, etc and output the length.
  • The program will always be valid and have only those 8 chars in it.

Unary -> Brainfuck Specs

  • You will have to implement the reverse of the above algorithm.
  • Again because of the huge sizes in question, the input will be a number describing the length of the Unary code.
  • Same I/O rules as always.
  • The program will always be valid and have only those 8 chars in it.

Test Cases

  • Hello World - ++++++[>++++++++++++<-]>.>++++++++++[>++++++++++<-]>+.+++++++..+++.>++++[>+++++++++++<-]>.<+++[>----<-]>.<<<<<+++[>+++++<-]>.>>.+++.------.--------.>>+. = 239234107117088762456728667968602154633390994619022073954825877681363348343524058579165785448174718768772358485472231582844556848101441556
  • Fibonacci - ++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>++++++++++++++++>>+<<[>>>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>[<+>-]>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]<[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]<<<++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<.>.>>[>>+<<-]>[>+<<+>-]>[<+>-]<<<-]<<++... = 13067995222095367150854793937817629722033205198624522624687536186118993888926522550140580142585590431635487113180955099384652678100247403485397450658564826143160529351955621991895221530908461364045400531236124980271740502887704217664044858614821622360156740992393765239123681327824577149595724956207165558106099868913919959549896553103116795519592552089266360725543244154867904980260

This is code-golf so lowest score in bytes wins!

Anyone up for a solution in Unary? ;P

  • 5
    A more appropriate title would probably be "Brainfuck to Golunar and Back" – Sp3000 Jul 7 '15 at 4:32
  • @Sp3000 good point, but I think that most people haven't really heard of it that one (myself included). – Maltysen Jul 7 '15 at 4:36
  • @Maltysen I don't think your test cases are correct. For instance, the leading digits of the first number in binary are 10101010101010, when they should be 1010010010010 – isaacg Jul 7 '15 at 5:16
  • @isaacg sorry, got them off a website that was using a different translation mechanism, will fix. – Maltysen Jul 7 '15 at 6:55
  • 1
    Can we convert them to a program which isn't exactly equal, but does exactly the same thing? – jimmy23013 Jul 7 '15 at 13:28

Pyth, 17 + 17 = 34 bytes

BF -> Unary, 17 bytes

i+1xL"><+-.,[]"z8

Unary -> BF, 17 bytes

s@L"><+-.,[]"tjQ8

Python 2, 80 79 63 55 + 86 64 = 119 Bytes

Thanks to Sp3000 for his numerous suggestions, saving a lot of bytes.

Brainfuck to Unary, 78 77 61 53 + 2 = 55 bytes

Added two bytes to account for surrounding "s on input.

print int(`[1]+map("><+-.,[]".find,input())`[1::3],8)

Unary to Brainfuck, 86 64 bytes

print''.join("><+-.,[]"[int(i)]for i in oct(input())[2:]if'L'>i)

Check it out on ideone here.

CJam, 35 bytes

Brainfuck to Unary, 17 bytes

1r"><+-.,[]"f#+8b

Try it online.

How it works

 r                e# Read a whitespace-separated token from STDIN.
            f     e# For each character in the token:
  "><+-.,[]" #    e#     Find its index in this string.
1             +   e# Prepend a 1 to the results.
               8b e# Convert to integer, using base 8 conversion.

Unary to Brainfuck, 18 bytes

ri8b"><+-.,[]"f=1>

Try it online.

How it works

r                  e# Read a whitespace separated token from STDIN.
 i                 e# Interpret as integer.
  8b               e# Convert to array using base 8 conversion.
              f    e# For each digit:
    "><+-.,[]" =   e#     Select the corresponding character from the string.
                1> e# Discard the first character.

Bash + coreutils, 39 + 47 = 86

b2u.sh:

dc -e8i1`tr '<>+-.,[]' 0-7`p|tr -dc 0-9

u2b.sh:

dc -e8o?p|tr -dc 0-9|tr 0-7 '<>+-.,[]'|cut -c2-

Test output:

$ echo "++++++[>++++++++++++<-]>.>++++++++++[>++++++++++<-]>+.+++++++..+++.>++++[>+++++++++++<-]>.<+++[>----<-]>.<<<<<+++[>+++++<-]>.>>.+++.------.--------.>>+." | ./b2u.sh
239234206933197750788456456928845900180965531636435002144714670872282710109774487453364223333807054152602699434658684117337034763550216789 
$ echo 239234206933197750788456456928845900180965531636435002144714670872282710109774487453364223333807054152602699434658684117337034763550216789 | ./u2b.sh
++++++[>++++++++++++<-[>.>++++++++++[>++++++++++<-[>+.+++++++..+++.>++++[>+++++++++++<-[>.<+++[>----<-[>.<<<<<+++[>+++++<-[>.>>.+++.------.--------.>>+.
$
  • 1
    tr -dc 0-9 (and in code golf you can assume that ? is fine unescaped) – izabera Jul 7 '15 at 17:34

05AB1E, 33 (17 + 16) bytes

Brainfuck to Unary-length:

"><+-.,[]"sSk1š8β

Try it online or verify all test cases.

Explanation:

"><+-.,[]"           # Push string "><+-.,[]"
          s          # Swap to take the (implicit) input
           S         # Convert it to a list of characters
            k        # Check for each the index in the string
             1š      # Prepend a 1 to the list of indices
               8β    # Convert the list to Base-8 (and output implicitly)

Unary-length to Brainfuck

8в¦"><+-.,[]"sèJ

Try it online or verify all test cases.

Explanation:

8в                  # Convert the (implicit) input-list from Base-8 to Base-10
  ¦                 # Remove the first 1
   "><+-.,[]"       # Push string "><+-.,[]"
             s      # Swap the list and string on the stack
              è     # Index each integer into this string
               J    # Join everything together (and output implicitly)

Dart, 77 + 142 = 219 bytes

f(s)=>BigInt.parse('1'+s.split('').map('><+-.,[]'.indexOf).join(''),radix:8);

F(n)=>BigInt.parse(n).toRadixString(8).split('').map((s)=>'><+-.,[]'.substring(int.parse(s),int.parse(s)+1)).join('').toString().substring(1);

Try it online!

Japt, 13 + 13 = 26 bytes

Brainfuck to Unary

i< n"><+-.,[]

Try it!

Explanation:

i<               :Insert a "<" at the start of the string (representing 1)
   n             :Convert string to decimal by interpreting as:
    "><+-.,[]    : A base 8 number represented by the 8 characters of BF

Unary to Brainfuck

s"><+-.,[]" Å

Try it!

Explanation:

s                :Convert decimal to string representation of:
 "><+-.,[]"      : Base 8 using the BF characters to represent the 8 digits
            Å    :Remove the extra "<" at the front

Notes

I can't find the meta post, but if my memory serves correctly answers are allowed to limit I/O to numbers that their language can support, as long as they implement an algorithm that would work if the language starts supporting larger numbers. That is the case here, Japt's ability to treat a string as "base n using these n characters for the digits" can only use the number data type on the other side of the operation, and thus the test cases won't actually run successfully; the output of the first program and the input of the second program will coerce the number to one which can be represented as a number, rather than using the actual number. For numbers which can be perfectly represented by Japt's number data type these programs will work as desired, and if the number data type changes to support larger numbers then these programs will start supporting those numbers as well.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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