Skip to content

Instantly share code, notes, and snippets.

@olligobber
Last active November 18, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olligobber/fcd38a9e5c24655722ed52b338b90093 to your computer and use it in GitHub Desktop.
Save olligobber/fcd38a9e5c24655722ed52b338b90093 to your computer and use it in GitHub Desktop.
Given a stream of bytes as input, each pair is ordered so the smallest is output first and the largest is output second. Uses 6 bytes of memory.
LOOP FOREVER
+[-
LOAD FIRST INPUT IN POS 0 & SECOND IN POS 1
,>,<
WHILE POS 0 ISNT 0
[
MOVE TO POS 1 & COPY VALUE TO POS 3 & 4
>[->>+>+>+<<<<]>>>>[-<<<<+>>>>]<<<<
MOVE TO POS 3
>>
IF POS 3 ISNT 0
[
SUBTRACT ONE FROM POS 4
>-<
SUBTRACT POS 3 FROM POS 4
[->-<]
END IF
]
ADD ONE TO POS 4
>+<
NOW POS 4 = ISZERO(POS 1)
IF ISZERO(POS 1)
>[
MOVE TO POS 0
<<<<
MOVE POS 0 TO POS 1
[->+<]
INCREMENT POS 0 & 1 & DECREMENT POS 2
+>+>-<<
RETURN TO POS 4
>>>>
RESET POS 4 TO 0
-
END IF
]<
MOVE TO POS 0
<<<
SUBTRACT ONE FROM POS 0 & 1
->-<
ADD ONE TO POS 2
>>+<<
END WHILE
]
NOW POS 1 IS DIFF AND POS 2 IS MIN
OUTPUT POS 2
>>.<<
ADD POS 2 TO POS 1
>>[-<+>]<<
OUTPUT POS 1
>.<
RESET POS 1
>[-]<
END LOOP
+]
+[-,>,<[>[->>+>+>+<<<<]>>>>[-<<<<+>>>>]<<[>-<[->-<]]>+[<<<<[->+<]+>+>->>-]<<<<->->+<<]>>.[-<+>]<.[-]<+]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment