-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
214 lines (196 loc) · 4.44 KB
/
main.c
File metadata and controls
214 lines (196 loc) · 4.44 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// A snafu is just a 24 character string
typedef struct snafu {
char val[25];
} Snafu;
// A list of snafus
typedef struct snafulist {
Snafu* array;
size_t size;
size_t max;
} SnafuList;
// Computes a^b
// doesn't work for negative powers
long lpow(long a, long b) {
// Stack vars
long orig = a;
// Return 0 if negative since we don't support them
if (b < 0) {
return 0;
}
// If power is 0 return 1
if (!b) {
return 1;
}
// Otherwise compute the value of a^b
for (long i=1; i<b; i++) {
a *= orig;
}
return a;
}
// Converts a snafu to a long
long snafutol(Snafu in) {
// Stack vars
long result = 0;
int curr = 0;
long marker = 0;
// Go backwards over the snafu
for (int i=strlen(in.val)-1; i>-1; i--) {
// Check what digit were on and set the marker
// appropriately
switch (in.val[i]) {
case '=':
marker = -2;
break;
case '-':
marker = -1;
break;
case '0':
marker = 0;
break;
case '1':
marker = 1;
break;
case '2':
marker = 2;
break;
}
// The value at this spot is the value of that digit
// multiplied by its current position in base 5
result += marker * lpow(5, curr++);
}
return result;
}
// Converts a long to a snafu
Snafu longtos(long in) {
// Stack vars
Snafu result;
Snafu output;
int curr = 0;
long digit = 0;
long carry = 0;
// Iterate until in is 0
while(in) {
// Calculate our digit which is the remainder of
// in divided by 5 and then add the carry bit
digit = (in % 5)+carry;
// Divide in by 5
in /= 5;
// Figure out what are value is and if we need to
// set the carry bit
switch (digit) {
case 0:
result.val[curr] = '0';
carry = 0;
break;
case 1:
result.val[curr] = '1';
carry = 0;
break;
case 2:
result.val[curr] = '2';
carry = 0;
break;
case 3:
result.val[curr] = '=';
carry = 1;
break;
case 4:
result.val[curr] = '-';
carry = 1;
break;
// Case 5 is in case the remainder was 4 but the carry
// bit is also set, we need to set our digit to 0 and
// set the carry bit again
case 5:
result.val[curr] = '0';
carry = 1;
break;
}
// Increment our current digit
curr++;
}
// Add another 1 to the end of result if the carry bit
// was set
if (carry) {
result.val[curr++] = '1';
}
// Null terminate result
result.val[curr] = 0;
// Our answer is actually backwrds so we need to reverse the result
// into the output variable
for (int i=0; i<strlen(result.val); i++) {
output.val[strlen(result.val)-i-1] = result.val[i];
}
// Null terminate output
output.val[curr] = 0;
return output;
}
// Solve the sum
Snafu solve(SnafuList snafus) {
// Stack var
long sum = 0;
// For each snafu convert it to a long
// and add it to the sum
for (int i=0; i<snafus.size; i++) {
sum += snafutol(snafus.array[i]);
}
// Return the sum as a snafu
return longtos(sum);
}
// Processes the input file and stores the result in the given
// snafulist
void processinput(char* filename, SnafuList* output) {
// Read in our file
FILE *in_file = fopen(filename, "r");
if (in_file == NULL) {
printf("Could not open input file\n");
exit(EXIT_FAILURE);
}
// Stack vars
char buffer[100];
// Zero-initialize our snafulist
output->max = 0;
output->size = 0;
// Figure out size of snafulist
while(fgets(buffer, sizeof buffer, in_file) != NULL) {
// For every non-empty line add another snafu
if (buffer[0] != '\n') {
output->max++;
}
}
// Go back to the beginning of the file
rewind(in_file);
// Allocate our memory
output->array = malloc((sizeof *output->array)*output->max);
// Go back over the file line by line
while(fgets(buffer, sizeof buffer, in_file) != NULL) {
// Make sure the line is not blank
if (buffer[0] != '\n') {
// Copy the buffer into the value of the snafu
strncpy(output->array[output->size].val, buffer, strlen(buffer)-1);
// Null terminate the snafu value
output->array[output->size++].val[strlen(buffer)] = 0;
}
}
// Close our file in case we want to call this function again
fclose(in_file);
return;
}
int main(int argc, char *argv[])
{
// Stack vars
SnafuList snafus;
Snafu answer;
// Get our snafu list
processinput("input.txt", &snafus);
// Get the sum of all the snafus
answer = solve(snafus);
// Solve by printing out the snafu sum
printf("The sum in snafu is %s\n", answer.val);
// Free our memory
free(snafus.array);
return EXIT_SUCCESS;
}