Loading ...
Sorry, an error occurred while loading the content.

9208Re: JFF: A little Challenge

Expand Messages
  • Eric Cooper
    Jan 9, 2008
    • 0 Attachment
      This is a fun thread. BTW, I noticed that the latest version from Florent
      does not handle nested [...] loops properly. So it works on the "OCaml"
      program, but not on something like
      http://en.wikipedia.org/wiki/Brainfuck#Division

      Here is a version that *compiles* the BF program into a single OCaml
      closure, then applies it to the initial state. The unobfuscated version:

      let src = open_in Sys.argv.(1)

      let right (f, i) = f, i + 1
      let left (f, i) = f, i - 1

      let set f i x j = if j = i then x else f j

      let inc (f, i) = set f i (f i + 1), i
      let dec (f, i) = set f i (f i - 1), i
      let inp (f, i) = set f i (try input_byte stdin with End_of_file -> 0), i

      let cur (f, i) = f i

      let out s = output_byte stdout (cur s); s
      let rec loop p s = if cur s <> 0 then loop p (p s) else s

      let id x = x

      let (>>) f g x = g (f x)

      let rec compile () =
      let emit q = q >> compile () in
      try match (input_char src) with
      | '>' -> emit right
      | '<' -> emit left
      | '+' -> emit inc
      | '-' -> emit dec
      | '.' -> emit out
      | ',' -> emit inp
      | '[' -> emit (loop (compile ()))
      | ']' -> id
      | _ -> compile ()
      with End_of_file -> id

      let const c x = c

      let _ = (compile ()) (const 0, 0)

      And here is the 447-byte obfuscated version. (It doesn't bother to store 0 on
      EOF, but should otherwise be equivalent.)

      let(!)(i,f)=f i
      let r(i,f)=i+1,f
      let l(i,f)=i-1,f
      let u(i,f)x=i,fun j->if j=i then x else f j
      let rec y p v=if!v=0 then v else y p(p v)let a v=u v(!v+1)let s v=u v(!v-1)let o v=output_byte stdout!v;v
      let n v=u v(input_byte stdin)let f=open_in Sys.argv.(1)let i x=x
      let($)f g x=g(f x)let rec c()=let k f=f$c()in
      try match(input_byte f)with|62->k r|60->k l|43->k a|45->k s|46->k o|44->k n|91->k(y(c()))|93->i|_->c()with _->i
      let _=(c())(0,fun _->0)

      --
      Eric Cooper e c c @ c m u . e d u
    • Show all 20 messages in this topic