Quellcode durchsuchen

refactoring

master
red vor 4 Jahren
Ursprung
Commit
b2e1e49847
1 geänderte Dateien mit 27 neuen und 33 gelöschten Zeilen
  1. +27
    -33
      day3.c

+ 27
- 33
day3.c Datei anzeigen

@@ -76,54 +76,48 @@ int manhattan(struct Point a, struct Point b) {
return (abs(a.x - b.x) + abs(a.y - b.y));
}

//scan an input string, saving all the points to out. return the no. of points read
size_t scan_points(char* in, struct Point* out){
//sequence is assumed to start at the origin.
out[0] = ORIGIN;
size_t i = 1;
while (*in != 0) { //read until null
char dir;
int delta;
int num_read;
sscanf(in, "%c%d%n", &dir, &delta, &num_read);
out[i] = get_pt(dir, delta, out[i]);
i++;
in += num_read + 1; // skip the number we just read, and the separator
}
return i;
}

int main() {
//allocate an input string, and two arrays for input points
//we don't know the exact length of the arrays but we know it's smaller than 400
char* input_raw = calloc(1500,sizeof(char));
scanf("%s", input_raw);
struct Point* a = calloc(400, sizeof(struct Point**));
struct Point* b = calloc(400, sizeof(struct Point**));
//Both a and b should start at the origin.
a[0] = ORIGIN;
b[0] = ORIGIN;
size_t a_index = 1;
size_t b_index = 1;
struct Point a[400];
struct Point b[400];
char* ptr = input_raw;
while (*ptr != 0) { //read till null
char dir;
int delta;
int num_read;
sscanf(ptr, "%c%d%n", &dir, &delta, &num_read);
a[a_index] = get_pt(dir, delta, a[a_index-1]);
a_index++;
ptr += num_read + 1; // skip the number we just read, and the separator
}
size_t a_size = scan_points(ptr, a);
//clear the input buffer
memset((void*) input_raw, 0, sizeof(char)*1500);
//scan the second input sequence, reset var ptr to the beginning of the string, reset location to the origin
scanf("%s", input_raw);
ptr = input_raw;
while (*ptr != 0) { //read until null
char dir;
int delta;
int num_read;
sscanf(ptr, "%c%d%n", &dir, &delta, &num_read);
b[b_index] = get_pt(dir, delta, b[b_index-1]);
b_index++;
ptr += num_read + 1; // skip the number we just read and the separator
}
free((void*) input_raw); // we don't need the raw input anymore
size_t b_size = scan_points(ptr, b);
free((void*) input_raw);
/*
* we want to find collisions between a and
*/
int min = INT_MAX;
int min_delay = INT_MAX;
int a_delay = 0;
for (size_t i = 0; i < a_index; i++) {
}
for (size_t i = 0; i < b_index; i++) {
}
//iterate over each combination of two edges in array of points a and b
for (size_t i = 1; i < a_index; i++) {
for (size_t i = 1; i < a_size; i++) {
int b_delay = 0;
for (size_t j = 1; j < b_index; j++) {
for (size_t j = 1; j < b_size; j++) {
struct Point pt = intersects(a[i-1], a[i], b[j-1], b[j]);
int dist = manhattan(ORIGIN, pt);
if (dist > 0) {

Laden…
Abbrechen
Speichern