5/src/5.c
char *solve(struct input *input, int part) {
	char *answer;
	int jump, new_position, position, steps;
	struct input *jumps;

	// We're going to modify the input data, so we'll need our own copy
	jumps = malloc(sizeof(struct input));
	jumps->length = input->length;
	jumps->data = malloc(sizeof(int) * input->length);
	memcpy(jumps->data, input->data, sizeof(int) * input->length);

	position = 0;
	steps = 0;
	while (position >= 0 && position < jumps->length) {
		steps++;
		if (part == 2 && jumps->data[position] >= 3) {
			position += jumps->data[position]--;
		}
		else {
			position += jumps->data[position]++;
		}
	}

	free(jumps->data);
	free(jumps);

	answer = malloc(16);
	snprintf(answer, 16, "%d", steps);
	
	return answer;
}
// Parts 1 and 2 are similar enough to use the same function for both parts.
Parts 1 and 2 are similar enough to use the same function for both parts.
Parts 1 and 2 are similar enough to use the same function for both parts .
char *solve(struct input *input, int part) {
	char *answer;
	int jump, new_position, position, steps;
	struct input *jumps;
	jumps = malloc(sizeof(struct input));
	jumps->length = input->length;
	jumps->data = malloc(sizeof(int) * input->length);
	memcpy(jumps->data, input->data, sizeof(int) * input->length);
	position = 0;
	steps = 0;
	while (position >= 0 && position < jumps->length) {
		steps++;
		if (part == 2 && jumps->data[position] >= 3) {
			position += jumps->data[position]--;
		}
		else {
			position += jumps->data[position]++;
		}
	}
	free(jumps->data);
	free(jumps);
	answer = malloc(16);
	snprintf(answer, 16, "%d", steps);
	return answer;
}
char * solve ( struct input * input , int part ) { char * answer ; int jump , new_position , position , steps ; struct input * jumps ; jumps = malloc ( sizeof ( struct input ) ) ; jumps -> length = input -> length ; jumps -> data = malloc ( sizeof ( int ) * input -> length ) ; memcpy ( jumps -> data , input -> data , sizeof ( int ) * input -> length ) ; position = 0 ; steps = 0 ; while ( position >= 0 && position < jumps -> length ) { steps ++ ; if ( part == 2 && jumps -> data [ position ] >= 3 ) { position += jumps -> data [ position ] -- ; } else { position += jumps -> data [ position ] ++ ; } } free ( jumps -> data ) ; free ( jumps ) ; answer = malloc ( 16 ) ; snprintf ( answer , 16 , " " , steps ) ; return answer ; }
Parts 1 and 2 are similar enough to use the same function for both parts.
Parts 1 and 2 are similar enough to use the same function for both parts .
// We're going to modify the input data, so we'll need our own copy