Skip to content

Instantly share code, notes, and snippets.

@ghoseb
Forked from alandipert/bottles.clj
Created September 8, 2013 06:13
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 ghoseb/6482336 to your computer and use it in GitHub Desktop.
Save ghoseb/6482336 to your computer and use it in GitHub Desktop.
;;; 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