Skip to content

Commit ff0a809

Browse files
authored
Merge pull request #51 from saxbophone/josh/spiral-collides-small-perf-increase
Make spiral_collides ignore the two lines before the last one
2 parents 8fdf656 + fb75f81 commit ff0a809

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

saxbospiral/solve.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ spiral_collides(spiral_t spiral, size_t index) {
3737
} else {
3838
// initialise a counter to keep track of what line we're on
3939
int64_t line_count = 0;
40-
// TODO: Check that the ttl needs to be length+1 and not just length
4140
int64_t ttl = spiral.lines[line_count].length + 1; // ttl of line
42-
/*
43-
* TODO: Make the loop ignore the two lines before the latest.
44-
* These can NEVER collide with it and this will improve performance.
45-
*/
4641
size_t last_co_ord = spiral.co_ord_cache.co_ords.size;
4742
line_t last_line = spiral.lines[index];
4843
size_t start_of_last_line = (last_co_ord - last_line.length) - 1;
@@ -69,6 +64,14 @@ spiral_collides(spiral_t spiral, size_t index) {
6964
line_count++;
7065
ttl = spiral.lines[line_count].length;
7166
}
67+
/*
68+
* terminate the loop if the next line would be the line 2 lines
69+
* before the last one (these two lines can never collide with the
70+
* last and can be safely ignored, for a small performance increase)
71+
*/
72+
if(line_count == (spiral.size - 2 - 1)) { // -1 for zero-index
73+
break;
74+
}
7275
}
7376
return -1;
7477
}

0 commit comments

Comments
 (0)