u/Kivooeo1

▲ 214 r/rust

Hey, I feel like I stuck a little with this xd

fn main(){for x in std::env::args().skip(1){let(mut t,mut p,mut i,b)=([0u8;999],0,0,x.as_bytes());
while i<b.len(){match b[i]{62=>p+=1,60=>p-=1,43=>t[p]+=1,45=>t[p]-=1,46=>print!("{}",t[p]as char),
91|93 if(b[i]<92)^(t[p]>0)=>{let(f,mut d)=(b[i]as i32-92,1);while d>0{i=(i as i32-f)as usize;
d+=match b[i]{91=>-f,93=>f,_=>0}}}_=>()}i+=1}}}

more readable version here:

fn main() {
    for x in std::env::args().skip(1) {
        let (mut t, mut p, mut i, b) = ([0u8; 999], 0, 0, x.as_bytes());
        while i < b.len() {
            match b[i] {
                62 => p += 1,
                60 => p -= 1,
                43 => t[p] += 1,
                45 => t[p] -= 1,
                46 => print!("{}", t[p] as char),
                91 | 93 if (b[i] < 92) ^ (t[p] > 0) => {
                    let (f, mut d) = (b[i] as i32 - 92, 1);
                    while d > 0 {
                        i = (i as i32 - f) as usize;
                        d += f * ((b[i] | 2) == 93) as i32
                    }
                }
                _ => (),
            }
            i += 1
        }
    }
}

Kinda proud about this trick in that arm

91|93 if(b[i]<92)^(t[p]>0)

xor here let me handle both [ and ] in a single match arm

I got an inspiration for doing this today by seeing once again a legendary C 160 bytes version, so I decided to try Rust's best to do something similar

Funny thing I've noticed: LLMs are surprisingly bad at it this, when I asked them to minimize code, every suggestion they gave was actually longer than what I originally gave them

Ok nevermind, actually, a couple of things, first is:

std::env::args().skip(1)

is pretty long, i'm actually unsure if there other way to read it smaller

second:

print!("{}",t[p]as char)

and this fancy arm here:

                91 | 93 if (b[i] < 92) ^ (t[p] > 0) => {
                    let (f, mut d) = (b[i] as i32 - 92, 1);
                    while d > 0 {
                        i = (i as i32 - f) as usize;
                        d += f * ((b[i] | 2) == 93) as i32
                    }
                }

i believe this match here could be written without match, but I can't prove it

upd1: u/AhoyISki now is 333 bytes!

upd2: actually u/binoboth version is 314 (π!!!) bytes, it's uses pattern binding @, same as u/wibble13 suggests earlier and interesting tech with i += f instead of mine i = (i as i32 - f) as usize, which saves a lot. also it drops u8 from [0u8; 999]

u/Kivooeo1 — 17 days ago