annotate crunchfuck.c @ 4846:5d23080c25e9

<mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
author HackBot
date Fri, 12 Sep 2014 13:59:37 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4846
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
1 /*
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
2 Copyright (c) 2012, Roman Muentener, fmnssun _at_ gmail [dot] com
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
3
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
4 Permission to use, copy, modify, and/or distribute this software for any
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
5 purpose with or without fee is hereby granted, provided that the above
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
6 copyright notice and this permission notice appear in all copies.
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
7
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
10 AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
11 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
12 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
13 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
14 PERFORMANCE OF THIS SOFTWARE.
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
15 */
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
16
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
17 #include <stdlib.h>
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
18 #include <stdio.h>
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
19 #include <string.h>
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
20 #include <assert.h>
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
21 #include <limits.h>
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
22
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
23 int valid_bf(char* program);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
24 char* next_bf(char* program, int pos);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
25 void interpret(char* program, int* solutions);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
26 inline int mod(int a, int b);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
27
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
28 #define PROG_MAX_LEN 40
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
29 #define PROG_MAX_STEPS 5000
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
30
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
31 int main(int argc, char* argv[])
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
32 {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
33 printf(" _ \n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
34 printf(" / `_ _ _ /__/| _ /_\n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
35 printf("/_,//_// //_ / // /_//_ /\\ \n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
36 printf("\n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
37
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
38
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
39 char* program;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
40 int* solutions;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
41
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
42 if(argc != 3) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
43 printf("Usage: %s prog n\n", argv[0]);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
44 printf("\tprog - A start program (example: ++++++++)\n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
45 printf("\tn - How many (next) programs you want to check\n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
46 exit(1);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
47 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
48
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
49 fflush(stdout);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
50
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
51 program = calloc(PROG_MAX_LEN, sizeof(char));
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
52 assert(program != NULL);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
53
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
54 solutions = calloc(265, sizeof(int));
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
55 assert(solutions != NULL);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
56
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
57 strncpy(program, argv[1], PROG_MAX_LEN - 1);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
58
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
59 unsigned long int limit = strtoul(argv[2], NULL, 10), i = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
60 assert(limit > 0);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
61
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
62 //printf("Reading a list of already known solutions for...\n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
63 /*while(1) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
64 int d;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
65 if(scanf("%d",&d) == 1) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
66 solutions[d] = 1;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
67 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
68 else break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
69 }*/
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
70 //printf("Done reading...\n");
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
71
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
72
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
73 for(;i < limit;i++) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
74 if(valid_bf(program))
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
75 interpret(program, solutions);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
76 program = next_bf(program, 0);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
77 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
78
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
79 printf("Last program: %s\n", program);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
80 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
81
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
82 inline int mod(int a, int b) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
83 if(a > b)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
84 return a - b;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
85 if(a < 0)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
86 return a + b;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
87 return a;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
88 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
89
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
90 void interpret(char* program, int* solutions)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
91 {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
92 unsigned char memory[256] = {0};
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
93 short iptr = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
94 unsigned char cptr = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
95 short steps = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
96 short len = strlen(program);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
97
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
98 while(iptr < len) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
99 if(steps > PROG_MAX_STEPS) return;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
100 steps++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
101
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
102 //printf("%d %c (iptr := %d, cptr := %d)\n",steps, program[iptr], iptr, cptr);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
103
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
104 switch(program[iptr]) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
105 case '+':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
106 memory[cptr]++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
107 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
108 case '-':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
109 memory[cptr]--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
110 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
111 case '>':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
112 cptr++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
113 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
114 case '<':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
115 cptr--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
116 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
117 case '[':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
118 if(memory[cptr] == 0) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
119 int lvl = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
120 iptr++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
121 while(iptr < len) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
122 if(program[iptr] == ']')
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
123 if(lvl < 1) break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
124 else lvl--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
125 if(program[iptr] == '[')
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
126 lvl++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
127 iptr++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
128 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
129 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
130 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
131 case ']':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
132 if(memory[cptr] != 0) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
133 int lvl = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
134 iptr--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
135 while(iptr >= 0) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
136 if(program[iptr] == '[')
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
137 if(lvl < 1) break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
138 else lvl--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
139 if(program[iptr] == ']')
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
140 lvl++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
141 iptr--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
142 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
143 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
144 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
145 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
146
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
147 iptr++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
148 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
149
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
150 if(!solutions[memory[cptr]]) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
151 printf("Result: %u -> %s\n", memory[cptr], program);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
152 fflush(stdout);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
153 solutions[memory[cptr]] = 1;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
154 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
155 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
156
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
157 char* next_bf(char* program, int pos)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
158 {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
159 if(pos >= PROG_MAX_LEN) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
160 fputs("Presumed maximum length exceeded!\n", stderr);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
161 exit(1);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
162 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
163
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
164 /* Next instruction */
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
165 switch(program[pos]) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
166 case '\0':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
167 program[pos] = '+';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
168 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
169 case '+':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
170 program[pos] = '-';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
171 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
172 case '-':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
173 program[pos] = '>';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
174 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
175 case '>':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
176 program[pos] = '<';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
177 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
178 case '<':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
179 program[pos] = '[';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
180 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
181 case '[':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
182 program[pos] = ']';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
183 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
184 case ']':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
185 program[pos] = '+';
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
186 return next_bf(program, pos+1);
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
187 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
188
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
189 return program;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
190 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
191
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
192 int valid_bf(char* program)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
193 {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
194 int level = 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
195 while(*program) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
196 char c = *program;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
197 switch(c) {
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
198 case '[':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
199 level++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
200 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
201 case ']':
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
202 level--;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
203 break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
204 default: break;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
205 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
206
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
207 if(level < 0)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
208 return 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
209
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
210 program++;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
211 }
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
212
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
213 if(level != 0)
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
214 return 0;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
215
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
216 return 1;
5d23080c25e9 <mroman_> fetch http://eso.mroman.ch/crunchfuck/crunchfuck.c
HackBot
parents:
diff changeset
217 }