Skip to main content
1 of 5
Mitch Schwartz
  • 5.9k
  • 1
  • 27
  • 26

brainfuck, 99 bytes

+>>>+[[<+>>+<-]++[>-<------]>-[>]<<[>>+>]>,]++++++++++[[[<]<]>>[-[>[.>]]<[<]>+[>
]>]<<.[<<[<]>-]++>]

Formatted:

+>>>+
[
  [<+> >+<-]
  ++[>-<------]>-[>]
  <<[>>+>]
  >,
]
++++++++++
[
  [[<]<]
  >>
  print
  [
    -[>[.>]]
    <[<]
    >+[>]
    >
  ]
  <<.
  increment
  [
    <<[<]
    >-
  ]
  ++>
]

Expects input of the form 1,23,4 without a trailing newline (the language doesn't allow input via command line arguments), and outputs the subsets in the same format (sometimes with a trailing comma, as allowed by the spec) on separate lines. The first line printed will always be empty, signifying the empty set.

Try it online.

The basic idea is to place a bit next to each element, then repeatedly increment the binary number while printing the corresponding subset before each increment. A sentinel bit is placed at the beginning to facilitate terminating the program.

Each bit is encoded as one plus its value; i.e., it can be either 1 or 2. The tape is laid out with the bit before each element and a single zero cell between each pair of elements. The comma is included on the tape for non-final elements, so that we don't need to do anything special to print delimiters.

Mitch Schwartz
  • 5.9k
  • 1
  • 27
  • 26