Make an application that displays text at random that conforms to the specified regex

后端 未结 11 726
情书的邮戳
情书的邮戳 2021-01-30 05:39

OK, so in the spirit of Code-Golf, I\'m trying out something new here: Code-Bowling.

In golf, you try to get the lowest score (smallest application, mos

11条回答
  •  -上瘾入骨i
    2021-01-30 06:07

    I think this set some records in WTF-iness. It's one function that compiles a human-readable assembly into BrainF***, and then another function that interprets it.

    Of course, the human-readable assembly is missing some features, so there's some hack-ish insertion of BF code manually. And the code is just generally full of WTFs.

    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
        import java.io.ByteArrayInputStream;
    import java.io.FileReader;
        import java.io.IOException;
    import java.io.InputStream;import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.StringReader;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.Stack;
    import java.util.StringTokenizer;
    
    
    public class CodeBowling {
        public static void main(String[] args) throws IOException{
            //An extended version of BrainF*ck, with a command for jumping a rand(int) to the right
            //I've heard that assembly languages always run faster, so this should be really efficient!
            String readable="";
            readable+="set 0 G\n";
            readable+="set 1 o\n";
            readable+="set 2 o\n";
            readable+="set 3 d\n";
            readable+="set 4 _\n";
    
            readable+="set 5 M\n";
            readable+="set 9 o\n";
            readable+="set 13 r\n";
            readable+="set 17 n\n";
            readable+="set 21 i\n";
            readable+="set 25 n\n";
            readable+="set 29 g\n";
    
            readable+="set 6 A\n";
            readable+="set 10 f\n";
            readable+="set 14 t\n";
            readable+="set 18 e\n";
            readable+="set 22 r\n";
            readable+="set 26 n\n";
            readable+="set 30 o\n";
            readable+="set 34 o\n";
            readable+="set 38 n\n";
    
            readable+="set 7 E\n";
            readable+="set 11 v\n";
            readable+="set 15 e\n";
            readable+="set 19 n\n";
            readable+="set 23 i\n";
            readable+="set 27 n\n";
            readable+="set 31 g\n";
    
            readable+="set 8 N\n";
            readable+="set 12 i\n";
            readable+="set 16 g\n";
            readable+="set 20 h\n";
            readable+="set 24 t\n";
    
            //Begin execution.
            readable+="print 0\n";
            readable+="print 1\n";
            readable+="print 2\n";
            readable+="print 3\n";
            readable+="sub 4 63\n";
            readable+="print 4\n";
    
            //VERY IMPORTANT
            //JUMP COMMANDS PERMANTENTLY SHIFT THE MEMORY.
            //DO NOT FOLLOW THEM BY ANY OTHER COMMANDS.
            readable+="rand\n";
            readable+="rand\n";
            readable+="rand";
    
            String bf=compile(readable);
    
            //Prints out the random greeting; the assembly does not include this function.
            //A request has been filed to the other developer to add this feature to the
            //compiler ASAP.
            bf+=">>>>>[.>>>>]"; 
    
            execute(bf);
        }
        static void execute(String program){
            InputStream is=null;
            try {
                is = new ByteArrayInputStream(program.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Scanner scn=new Scanner(is);
            List acceptedChars=Arrays.asList('<','>','.',',','+','-','[',']','J');
            int brack=0;
            Stack brackStack=new Stack();
            Map brackMap=new HashMap();
            int pos=0;
            StringBuilder sb=new StringBuilder();
                        scn.useDelimiter("");
                int warnings=0;
                while(scn.hasNext()){
                    char nextChar=scn.next().charAt(0);
                    if(acceptedChars.contains(nextChar)){
                        sb.append(nextChar);
                        if(nextChar=='['){
                            brack++;
                            brackStack.push(pos);
                        }
                        if(nextChar==']'){
                            brack--;
                            brackMap.put(pos, brackStack.peek());
                            brackMap.put(brackStack.pop(), pos);
                        }
                    } else if(warnings<3){
                        System.out.println("Warning: unrecognized character '"+((nextChar=='\r'||nextChar=='\n')?"newline":nextChar)+"'");
                        warnings++;
                        if(warnings==3)
                            System.out.println("Too many warnings, suppressing output.");
                    }
                    pos++;
                }
            if(brack!=0){
                System.out.println("Error: unbalanced brackets.");
                System.exit(1);
            }
            char[] pgrm=sb.toString().toCharArray();
    
            //Begin execution
            int Codeloc=0,Memloc=0,lim=pgrm.length;
            ArrayList mem=new ArrayList();
            scn=new Scanner(System.in);
            scn.useDelimiter("");
    
            while(Codeloc':
                        Memloc++;
                        break;
                    case '<':
                        Memloc--;
                        break;
                    case '[':
                        if(mem.get(Memloc)==0){
                            Codeloc=brackMap.get(Codeloc);
                        } else {
                            Codeloc=Codeloc;//0 //brackStack.peek() //Codeloc++;;;
                        }
                        break;
                    case ']':
                        Codeloc=brackMap.get(Codeloc)-1;
                        break;
                    case '.':
                        System.out.print((char)(int)mem.get(Memloc));
                        break;
                    case ',':
                        mem.set(Memloc,(int)scn.next().charAt(0));
                        break;
                    case 'J':
                        java.util.Random r=new java.util.Random();
                        Memloc+=r.nextBoolean()?1:0;
                    }
                    Codeloc++;
                } catch (java.lang.IndexOutOfBoundsException e){
                    //i++;
                    //if(i>20){
                    //  System.exit(1);
                    //}
                    mem.add(0);
                }
            }
        }
        static String compile(String readable) throws IOException{
            String program="";
            BufferedReader is=null;
            try {
                is = new BufferedReader((Reader)new StringReader(readable));
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            Stack continueLoopingWhileNotEqualToZeroLocations=new Stack();
            while(is.ready()){
                String command=is.readLine();
                try{
                    int a=1/((command==null)?0:1);
                } catch (Exception e) {
                    break;
                }
                StringTokenizer st=new StringTokenizer(command);
                String type=st.nextToken();
                if(type.equals("set")){ // "set 4 c" sets memory location 4 to a "c".
                    Object temp=new Integer(st.nextToken());
                    int location=(Integer)temp;
                    for(int i=0;i0;i--){
                        program+="<";
                    }
                } else if(type.equals("add")){ // "add 4 10" increments memory location 4 by 10.
                    Object temp=new Integer(st.nextToken());
                    int location=(Integer)temp;
                    for(int i=0;i0;i--){
                        program+="<";
                    }
                } else if(type.equals("sub")){ // "sub 4 10" decrements memory location 4 by 10.
                    Object temp=new Integer(st.nextToken());
                    int location=(Integer)temp;
                    for(int i=0;i0;i--){
                        program+="<";
                    }
                } else if(type.equals("addFrom")){ // "addFrom 4 5 7 9" adds the value of location 4 to locations 5, 7, and 9, and erases 4.
                    Object temp=new Integer(st.nextToken());
                    int location=(Integer)temp;
                    for(int i=0;i0){
                            for(int i=0;i0){
                        for(int i=0;i0){
                            for(int i=0;i0){
                        for(int i=0;i)continueLoopingWhileNotEqualToZeroLocations).push(continueLoopingWhileNotEqualToZeroLocation);
                } else if(type.equals("endwhile")){
                    int l=((Stack)continueLoopingWhileNotEqualToZeroLocations).pop();
                    for(int i=l/2;i<-((l+1)/2);i--){
                        program+=">";
                    }
                } else if(type.equals("rand")){
                    program+="J";
                } else {
                    System.out.println("UNRECOGNIZED COMMAND "+type);
                    int a=1/0; //Kills the program.
                }
            }
            return program;
        }
    }
    

提交回复
热议问题