-
-
Save ghoseb/6482336 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 99 bottles of beer in Clojure | |
(def bottles " | |
# 99 Bottles of Beer | |
# Brainfuck version | |
# by Michal Wojciech Tarnowski | |
+>+++++++[>>>+++ | |
+++++<<<<+++++ | |
+++>-]+++++++++>>> | |
+>>++++++++++[>+ | |
+++++++++<-]>[ | |
>>+>+>+>+>+>+> | |
+>>+>+>>+>+>+> | |
+>>+>+>>+>+>+> | |
>+>+>+>+>>>>>+ | |
>+>+>+>>+>+>+> | |
>+>+>+>+>>+>+> | |
+>>+>+>+>+>>+>+> | |
>+>+>+>+>+>+>>>> | |
+>+>>+>+>+>+<<<< | |
<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<< | |
-]<++++[>++++++++< | |
-]>[>+>>>>>>>>+>>> | |
+>>>>>+>>>+>>>>+>> | |
>>>+>+>>+>>>>>+>>>>+ | |
>>>>>+>>>>+>>>>>+>>> | |
+>>>>>>>+>+>+>>>+>>> | |
>>+<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<-]+++++[ | |
>>>++>+++>+++>++>>+++>>++> | |
>>>>>+++>>++>++>>+++>+>>>+++ | |
+>->++>++>++>+++>++>>--->->+>>>+ | |
+>++>>>>++>++++>++>>->++>>>++>->++ | |
+>+++>>+>+++>>>+++>++>+++>++>>>++>>++> | |
++>>++>++>+++<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
<<<<-]>>-->+>+>+>-->+>>>+>++>>-->+>+>->>+>>> | |
+>->+>>->++>-->-->++>->>>->++>++>+>>+>>+>>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>.>.>.> | |
.>.>.>.>.>.>>>.<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
<<.>>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>>>>>>>>>> | |
>>>>.>.>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>. | |
>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
<<<<-.>>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>.>.>.>.>. | |
>.>.>.>.>.>.>.>.>>.>..<<<<<<<<<<<<<<<<<<<<<<<<<< | |
<<<<<<->]<<]<.>>>>.>.>.>.>.>.>.>>.>.>.>.>.>.>.>. | |
>.>.>.>.>.>.>.>.>.>.>.>.>.>>>.<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<.>>>>.>.>.>.>.>.>.>>.>.>.>.>.>. | |
>.>.>>>>>>>>>>>>>>.>.>>.>.>.>.>.>.>.>.>.>.>.>.>. | |
>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>. | |
>.>.>.>.>.<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | |
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<.>.>.>.>.>.>. | |
>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>>.>") | |
(defn bfc | |
[program] | |
(let [allowed #{\+ \- \< \> \[ \] \.} | |
src (->> program (filter allowed) (interpose \space) (apply str)) | |
fns (zipmap '(- + < > . ?) (repeatedly gensym))] | |
(letfn [(bfc* [s] | |
(if (vector? s) | |
`(while (not (~(fns '?))) ~@(map bfc* s)) | |
`(~(fns s))))] | |
`(let [tape# (int-array 60000 0) | |
pointer# (atom 0) | |
~(fns '-) #(aset tape# @pointer# (dec (aget tape# @pointer#))) | |
~(fns '+) #(aset tape# @pointer# (inc (aget tape# @pointer#))) | |
~(fns '<) #(swap! pointer# dec) | |
~(fns '>) #(swap! pointer# inc) | |
~(fns '.) #(print (char (aget tape# @pointer#))) | |
~(fns '?) #(zero? (aget tape# @pointer#))] | |
~@(map bfc* (read-string (str "(" src ")"))) nil)))) | |
(defn bfe [program] (eval (bfc program))) | |
(bfe bottles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment