-
Notifications
You must be signed in to change notification settings - Fork 1
/
car-parse.cc
160 lines (142 loc) · 2.9 KB
/
car-parse.cc
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
152
153
154
155
156
157
158
159
160
#include <cstdlib>
#include <cstdio>
#include <cassert>
typedef int trit;
const int NOTRITS = -1;
const int BADNUM = -2;
const bool echo = false;
int read_char() {
int c = getchar();
if (echo) {
printf("%c", c);
}
return c;
}
void skip() {
int c;
do {
c = read_char();
} while (c != '\n');
}
// read a trit, passing through whitespace, and dropping comments from # to eol
trit read_trit() {
while (!feof(stdin)) {
int c = read_char();
switch (c) {
case '0' :
case '1' :
case '2' :
return c;
case '#' :
skip();
return read_trit();
default :
if (!echo) {
printf("%c", c);
}
}
}
return NOTRITS;
}
// reads a structural number: encoded as in nums.list
int read_s_number() {
const int off[] = { 0, 3, 6, 15, 42, 123, 366, 1095 };
switch (read_trit()) {
case '0' : return 0;
case '1' : return 1;
case '2' : {
if (read_trit() != '2') {
//assert (false);
return BADNUM;
}
int ntrits = read_s_number();
if (ntrits < 0) {
skip();
return BADNUM;
}
//printf("looking for N trits: %d\n", ntrits);
if (ntrits == 0) {
return 2;
}
int num = 0;
for (int i=0; i<ntrits; i+=1) {
num *= 3;
num += (read_trit() - '0');
}
num += off[ntrits];
return num;
}
case NOTRITS: {
return NOTRITS;
}
default: assert(false);
}
}
// reads a fuel number: encoded as in nums.list
int read_f_number() {
switch (read_trit()) {
case '0' : return 0;
case '1' :
return (read_trit() - '0');
case '2' : {
if (read_trit() != '2') {
//assert (false);
return BADNUM;
}
int ntrits = read_f_number() + 2;
if (ntrits < 2) {
skip();
return BADNUM;
}
//printf("looking for N trits: %d\n", ntrits);
int num = 0;
for (int i=0; i<ntrits; i+=1) {
num *= 3;
num += (read_trit() - '0');
}
return num;
}
case NOTRITS: {
return NOTRITS;
}
default: assert(false);
}
}
int main(int argc, char* argv[]) {
int car = 0;
while (!feof(stdin)) {
car += 1;
printf("CAR %d\n", car);
int chambers = read_s_number();
printf("%d chambers.\n", chambers);
for (int c=0; c<chambers; c++) {
printf(" chamber %d -- \n", c);
int upipe = read_s_number();
printf(" upper (%d sections):", upipe);
for (int s=0; s<upipe; s++) {
int fuel = read_f_number();
assert(fuel<6);
printf(" %d", fuel + 1);
}
switch (read_trit()) {
case '0':
printf("\n MAIN\n");
break;
case '1':
printf("\n AUXILIARY\n");
break;
default: assert(false);
}
int lpipe = read_s_number();
printf(" lower (%d sections):", lpipe);
for (int s=0; s<lpipe; s++) {
int fuel = read_f_number();
assert (fuel<6);
printf(" %d", fuel + 1);
}
printf("\n");
}
printf("==================\n");
}
return EXIT_SUCCESS;
}