Re: "ocaml_beginners"::[] JFF: A little Challenge

Expand Messages
• ... Hi Fabrice ! ... thanks, and as you told me in pm, the bug that the cell wasn t checked at the beginning of a loop but at the end (since I had
Message 1 of 20 , Jan 1 5:20 AM
> Hi Florent !
Hi Fabrice !

> You've drastically reduce the size ! all that in nice purely functional
> style. Moreover the code isn't obfuscated anymore.
>
> A minor change : factoring "p" saves 16 bytes -> 413.

thanks,
and as you told me in pm, the bug that the cell wasn't checked at the
beginning of a loop but at the end (since I had misunderstood this point),
so I have corrected this

Now 446 bytes:

let i,f=input_char,open_in Sys.argv.(1)let rec p(c,b,k)=try
p(match i f with '+'->c+1,b,k|'-'->c-1,b,k|'<'->(match b
with[],r->0,([],c::r),k|h::l,r->h,(l,c::r),k)|'>'->(match b
with l,[]->0,(c::l,[]),k|l,h::r->h,(c::l,r),k)|'['->if
c=0then(while i f<>']'do()done;c,b,k)else c,b,pos_in
f-1::k|']'->(match k with[]->failwith"]"|h::t->seek_in
f h;c,b,t)|','->input_byte stdin,b,k|'.'->output_byte
stdout c;c,b,k|_->c,b,k)with _->();;p(0,([],[]),[])

Now who will write "OCaml" in bf with the minimum of bytes?
Here 83 bytes:
+++++++++[>+++++++++<-]>--.------------.<
<++++++[>+++++<-]>[->+<]>.++++++++++++.-.
• Could write : let rec p((c,b,k)as z)... - * 1 = + 6 bytes and replace c,b,k by z - (-4) * 3 = -12 bytes ... - 6 bytes Regards, Fabrice
Message 2 of 20 , Jan 2 2:00 PM
Could write :

let rec p((c,b,k)as z)... -> * 1 = + 6 bytes

and replace c,b,k by z -> (-4) * 3 = -12 bytes
------------
- 6 bytes

Regards,

Fabrice
• 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,
Message 3 of 20 , Jan 9 10:42 AM
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
• Hi Eric ! ... As far as I understand that is really beautiful. ... Nice idea to use ascii codes : 2 chars instead of 3, eight times ! Regards, Fabrice
Message 4 of 20 , Jan 10 12:26 PM
Hi Eric !

> 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

As far as I understand that is really beautiful.

> 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
Nice idea to use ascii codes : 2 chars instead of 3, eight times !

Regards,

Fabrice
• Hi Eric ! ... I only count 446. ... Using Florent s trick let z=input_byte 1* +17 and z vs input_byte - 2* 9 - -1 Noticing r\$l = i let i x=x
Message 5 of 20 , Jan 12 5:03 PM
Hi Eric !
> And here is the 447-byte obfuscated version.
I only count 446.

> 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)

Using Florent's trick "let z=input_byte " 1* +17 and "z" vs "input_byte" - 2* 9 -> -1
Noticing r\$l = i "let i x=x " 1* -10 and "r\$l" vs "i" + 2* 2 -> -6

Things becomes hard to save 7 poor bytes...

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 z=input_byte
let n v=u v(z stdin)
let f=open_in Sys.argv.(1)
let(\$)f g x=g(f x)
let rec c()=let k f=f\$c()in
try match(z 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->r\$l|_->c() with _->r\$l
let _=(c())(0,fun _->0)

439 bytes

Regards,

Fabrices
• ... Very nice observation! -- Eric Cooper e c c @ c m u . e d u
Message 6 of 20 , Jan 13 7:50 AM
On Sun, Jan 13, 2008 at 02:03:17AM +0100, Fabrice Marchant wrote:
> Noticing r\$l = i "let i x=x " 1* -10 and "r\$l" vs "i" + 2* 2 -> -6

Very nice observation!

--
Eric Cooper e c c @ c m u . e d u
• ... Hi Jon, I m unable to locate the mini-interpreter. Please could you give the link ? Many thanks, Fabrice
Message 7 of 20 , Mar 5, 2008
> You might like to look at the mini-interpreter I posted on the caml-list
> recently.
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/products/?e

Hi Jon,

I'm unable to locate the mini-interpreter. Please could you give the link ?

Many thanks,

Fabrice
• ... Try this: http://caml.inria.fr/pub/ml-archives/caml-list/2007/08/efef7a07b16bf89dafa4fddc4f8443d7.en.html -- Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Message 8 of 20 , Mar 5, 2008
On Wednesday 05 March 2008 19:49:03 Fabrice Marchant wrote:
> > You might like to look at the mini-interpreter I posted on the caml-list
> > recently.
> >
> > --
> > Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> > http://www.ffconsultancy.com/products/?e
>
> Hi Jon,
>
> I'm unable to locate the mini-interpreter. Please could you give the link
> ?

Try this:

http://caml.inria.fr/pub/ml-archives/caml-list/2007/08/efef7a07b16bf89dafa4fddc4f8443d7.en.html

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e
• On Wed, 5 Mar 2008 23:09:54 +0000 ... Thanks a lot !
Message 9 of 20 , Mar 8, 2008
On Wed, 5 Mar 2008 23:09:54 +0000
> > I'm unable to locate the mini-interpreter. Please could you give the link
> > ?
>
> Try this:
>
> http://caml.inria.fr/pub/ml-archives/caml-list/2007/08/efef7a07b16bf89dafa4fddc4f8443d7.en.html
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> http://www.ffconsultancy.com/products/?e

Thanks a lot !
Your message has been successfully submitted and would be delivered to recipients shortly.