Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created February 11, 2012 03:02
Show Gist options
  • Save alandipert/1795629 to your computer and use it in GitHub Desktop.
Save alandipert/1795629 to your computer and use it in GitHub Desktop.
99 bottles of beer in Clojure
;;; 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