-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
174 lines (169 loc) Β· 5.27 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
int main(int argc, char* argv[])
{
char const* const file_name = argv[1];
FILE* file = fopen(file_name, "r");
char line[2048];
fgets(line, sizeof(line), file);
// printf("%s", line);
int xvals[1024];
int yvals[1024];
int moves = extract_xy_moves(line, xvals, yvals);
int steps = absolute_sum(moves, xvals) + absolute_sum(moves, yvals);
// printf("steps: %d\n", steps);
int xlocations[steps+1];
int ylocations[steps+1];
set_xy_locations(moves, xvals, yvals, xlocations, ylocations);
fgets(line, sizeof(line), file);
fclose(file);
// printf("%s", line);
moves = extract_xy_moves(line, xvals, yvals);
int distance = compare_xy_locations(moves, xvals, yvals, steps+1, xlocations, ylocations);
printf("Distance: %d\n", distance);
}
int extract_xy_moves(char *input, int *xvals, int *yvals)
{
// printf("Length: %ld\n", strlen(input));
int input_index = 0;
int multiplier;
int vals_index = 0;
int * value_pointer;
int * zero_pointer;
while (input_index < strlen(input) - 1) {
// printf("RLUD? %c\n", input[input_index]);
if (strncmp(&input[input_index], "R", 1) == 0) {
multiplier = 1;
value_pointer = &xvals[vals_index];
zero_pointer = &yvals[vals_index];
} else if (strncmp(&input[input_index], "U", 1) == 0) {
multiplier = 1;
value_pointer = &yvals[vals_index];
zero_pointer = &xvals[vals_index];
} else if (strncmp(&input[input_index], "L", 1) == 0) {
multiplier = -1;
value_pointer = &xvals[vals_index];
zero_pointer = &yvals[vals_index];
} else {
multiplier = -1;
value_pointer = &yvals[vals_index];
zero_pointer = &xvals[vals_index];
}
input_index++;
int value = 0;
while (input_index < strlen(input) - 1 && strncmp(&input[input_index], ",", 1) != 0) {
// printf("value: %c\n", input[input_index]);
value = value * 10 + (input[input_index] - '0');
input_index++;
}
value = multiplier * value;
// printf("final value: %d\n", value);
*value_pointer = value;
*zero_pointer = 0;
input_index++;
vals_index++;
}
// printf("length: %d\n", vals_index);
// printf("xvals: ");
// for (int i = 0; i < vals_index; i++) {
// printf("%d ", xvals[i]);
// }
// printf("\n");
// printf("yvals: ");
// for (int i = 0; i < vals_index; i++) {
// printf("%d ", yvals[i]);
// }
// printf("\n");
return vals_index;
}
int absolute_sum(int length, int *array)
{
int result = 0;
for (int i=0; i<length; i++) {
result += abs(array[i]);
}
return result;
}
void set_xy_locations(int moves, int *xmoves, int *ymoves, int *xlocs, int *ylocs)
{
// printf("moves: %d\n", moves);
xlocs[0] = 0;
ylocs[0] = 0;
int index = 1;
int num_steps;
int* moves_loc;
int* still_loc;
for (int i=0; i<moves; i++) {
if (xmoves[i] != 0) {
// printf("x steps: %d\n", xmoves[i]);
num_steps = xmoves[i];
moves_loc = xlocs;
still_loc = ylocs;
} else {
// printf("y steps: %d\n", ymoves[i]);
num_steps = ymoves[i];
moves_loc = ylocs;
still_loc = xlocs;
}
for (int j=0; j<abs(num_steps); j++) {
moves_loc[index] = moves_loc[index-1] + num_steps / abs(num_steps);
still_loc[index] = still_loc[index-1];
index++;
}
}
// printf("length: %d\n", index);
// printf("xlocs: ");
// for (int i = 0; i < index; i++) {
// printf("%d ", xlocs[i]);
// }
// printf("\n");
// printf("ylocs: ");
// for (int i = 0; i < index; i++) {
// printf("%d ", ylocs[i]);
// }
// printf("\n");
}
int compare_xy_locations(int moves, int *xmoves, int *ymoves, int locations, int *xlocs, int *ylocs)
{
// printf("moves: %d\n", moves);
int xloc = 0;
int yloc = 0;
int index = 1;
int num_steps;
int* moves_loc;
int* still_loc;
int distance = 0;
int travel = 0;
for (int i=0; i<moves; i++) {
if (xmoves[i] != 0) {
// printf("x steps: %d\n", xmoves[i]);
num_steps = xmoves[i];
moves_loc = &xloc;
still_loc = &yloc;
} else {
// printf("y steps: %d\n", ymoves[i]);
num_steps = ymoves[i];
moves_loc = &yloc;
still_loc = &xloc;
}
for (int j=0; j<abs(num_steps); j++) {
*moves_loc += num_steps / abs(num_steps);
index++;
travel++;
for (int k=0; k<locations; k++) {
if (xlocs[k] == xloc && ylocs[k] == yloc) {
printf("Meet at (%d, %d) - travel distance of %d", xloc, yloc, k+travel);
if (distance == 0 || distance > k+travel) {
distance = k+travel;
printf(" - new minimum!\n");
} else {
printf("\n");
}
}
}
}
}
return distance;
}