1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
const char* next_word(void){
static char buf[1024];
int len=0;
char c=getchar();
while(isspace(c))c=getchar();
if(feof(stdin))return NULL;
if(!isalnum(c)){
buf[0]=c;
buf[1]='\0';
return buf;
}
while(isalnum(c)&&!feof(stdin)){
buf[len++]=c;
c=getchar();
}
if(!feof(stdin))ungetc(c,stdin);
buf[len]='\0';
return buf;
}
struct rule {
int state;
int direction; // right = 1, left = -1
bool value;
};
struct rules {
struct rule table[8][2];
int start;
int stopcount;
};
#define EQ(s1_,s2_) (strcmp((s1_),(s2_))==0)
#define READ(dest_) assert((dest_)=next_word())
#define READ_EOF(dest_) ((dest_)=next_word())
#define READSTATE(dest_) ((dest_)=next_word()[0]-'A')
#define READINT(dest_) ((dest_)=strtol(next_word(),NULL,10))
#define EXPECT(s_) assert(EQ(next_word(),(s_)))
static struct rules parse(void){
struct rules R;
int curstate=0,curval=0;
const char *word;
while(true){
READ_EOF(word);
if(!word)break;
if(EQ(word,"Begin")){
EXPECT("in"); EXPECT("state");
READSTATE(R.start);
EXPECT(".");
} else if(EQ(word,"Perform")){
EXPECT("a"); EXPECT("diagnostic"); EXPECT("checksum"); EXPECT("after");
READINT(R.stopcount);
EXPECT("steps"); EXPECT(".");
} else if(EQ(word,"In")){
EXPECT("state");
READSTATE(curstate);
EXPECT(":");
} else if(EQ(word,"If")){
EXPECT("the"); EXPECT("current"); EXPECT("value"); EXPECT("is");
READINT(curval);
EXPECT(":");
} else if(EQ(word,"-")){
READ(word);
if(EQ(word,"Write")){
EXPECT("the"); EXPECT("value");
READINT(R.table[curstate][curval].value);
EXPECT(".");
} else if(EQ(word,"Move")){
EXPECT("one"); EXPECT("slot"); EXPECT("to"); EXPECT("the");
READ(word);
if(EQ(word,"right"))R.table[curstate][curval].direction=1;
else if(EQ(word,"left"))R.table[curstate][curval].direction=-1;
else assert(false);
EXPECT(".");
} else if(EQ(word,"Continue")){
EXPECT("with"); EXPECT("state");
READSTATE(R.table[curstate][curval].state);
EXPECT(".");
} else {
assert(false);
}
} else {
assert(false);
}
}
return R;
}
#undef EQ
#undef EXPECT
static void printrules(const struct rules R){
printf("start=%c\nstopcount=%d\n",R.start+'A',R.stopcount);
for(int i=0;i<8;i++){
printf("%c: %d,%c,%c %d,%c,%c\n",
'A'+i,
R.table[i][0].value,"<>"[R.table[i][0].direction],R.table[i][0].state+'A',
R.table[i][1].value,"<>"[R.table[i][1].direction],R.table[i][1].state+'A');
}
}
int main(void){
freopen("25.in","r",stdin);
struct rules R=parse();
// printrules(R);
assert(sizeof(bool)==1);
int tapesize=4096;
bool *tape=calloc(tapesize,1);
int state=R.start,ptr=tapesize/2;
for(int iter=0;iter<R.stopcount;iter++){
if(ptr==-1){
bool *newtape=calloc(2*tapesize,1);
memcpy(newtape+tapesize,tape,tapesize);
ptr+=tapesize;
free(tape);
tape=newtape;
tapesize*=2;
} else if(ptr==tapesize){
bool *newtape=calloc(2*tapesize,1);
memcpy(newtape,tape,tapesize);
free(tape);
tape=newtape;
tapesize*=2;
}
struct rule *rule=&R.table[state][tape[ptr]];
tape[ptr]=rule->value;
ptr+=rule->direction;
state=rule->state;
}
int total=0;
for(int i=0;i<tapesize;i++)total+=tape[i];
printf("%d\n",total);
free(tape);
}
|