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
|
//
// Task 1: Special Bit Characters
//
// C version.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "args.h"
// decode( bitlist );
// apply the decoding rules to bitlist, modifying it.
//
void decode( char *bitlist )
{
// decode
bool changed = false;
do
{
changed = false;
// decode 10 -> b
char *p = strstr( bitlist, "10" );
if( p != NULL )
{
printf( "found 10 in bitlist at %s, rewriting as b\n", p );
*p = 'b';
// shift rest of bitlist up one pos
for( char *s = p+1; (*s = *(s+1)) != '\0'; s++ ) /*EMPTY*/;
printf( "shifted bitlist up one %s\n", bitlist );
changed = true;
continue;
}
// decode 11 -> c
p = strstr( bitlist, "11" );
if( p != NULL )
{
printf( "found 11 in bitlist at %s, rewriting as c\n", p );
*p = 'c';
// shift rest of bitlist up one pos
for( char *s = p+1; (*s = *(s+1)) != '\0'; s++ ) /*EMPTY*/;
printf( "shifted bitlist up one %s\n", bitlist );
changed = true;
continue;
}
// decode 0 -> a
p = strchr( bitlist, '0' );
if( p != NULL )
{
printf( "found 0 in bitlist at %s\n", p );
*p = 'a';
printf( "altered bitlist to %s\n", bitlist );
changed = true;
continue;
}
} while( changed );
printf( "modified bits: %s\n", bitlist );
}
int main( int argc, char **argv )
{
int argno = process_flag_n_args( "spc", argc, argv,
1, "bitlist" );
char *bitlist = argv[argno];
if( debug )
{
printf( "bitlist: %s\n", bitlist );
}
bool ok = true;
char badbit = 'a';
for( char *s=bitlist; *s; s++ )
{
if( *s != '0' && *s != '1' )
{
ok = false;
badbit = *s;
}
}
if( ! ok )
{
fprintf( stderr, "bad bit %c in %s\n", badbit, bitlist );
exit(1);
}
decode( bitlist );
if( debug )
{
printf( "decoded bits: %s\n", bitlist );
}
int len = strlen(bitlist);
printf( "%d\n", bitlist[len-1] == 'a' ? 1 : 0 );
return 0;
}
|