Skip to content

Commit

Permalink
Compress code to the extent that's still readable
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 4, 2024
1 parent f744119 commit cc496ea
Show file tree
Hide file tree
Showing 82 changed files with 167 additions and 481 deletions.
8 changes: 8 additions & 0 deletions c/src/include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@
#define POW_OF_MAX_POW_10_64 19
#define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64)
#define POW_OF_MAX_POW_10_128 38

#define PROGRAM_TAIL(type, prob) \
#ifndef UNITY_END \
int main(int argc, char const *argv[]) { \
printf("%" type "\n", prob());
return 0; \
} \
#endif
6 changes: 2 additions & 4 deletions c/src/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) {
for (i = 2; i <= n - r; i++)
factors[i] -= 1;
// this loop reduces to prime factors only
for (i = n; i > 1; i--) {
for (j = 2; j < i; j++) {
for (i = n; i > 1; i--)
for (j = 2; j < i; j++)
if (i % j == 0) {
factors[j] += factors[i];
factors[i / j] += factors[i];
factors[i] = 0;
break;
}
}
}
i = j = 2;
answer = 1;
while (i <= n) {
Expand Down
12 changes: 4 additions & 8 deletions c/src/include/primes.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ uintmax_t advance_prime_counter(prime_counter *pc) {
}
for (uintmax_t p = prime_cache[pc->idx - 1] + 2; p < pc->stop; p += 2) {
bool broken = false;
for (size_t idx = 1; idx < prime_cache_idx; idx++) {
for (size_t idx = 1; idx < prime_cache_idx; idx++)
if (p % prime_cache[idx] == 0) { // is not prime
broken = true;
break;
}
}
if (!broken) { // primeness not determined, exceeded cache
uintmax_t root_p = ceil(sqrt(p));
for (uintmax_t c = prime_cache_max; c <= root_p; c += 2) {
Expand All @@ -81,7 +80,7 @@ uintmax_t advance_prime_counter(prime_counter *pc) {
}
}
if (!broken) { // is prime
if (pc->idx == prime_cache_idx) {
if (pc->idx == prime_cache_idx)
#ifdef PRIME_CACHE_SIZE_LIMIT
if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT)
#else
Expand All @@ -102,7 +101,6 @@ uintmax_t advance_prime_counter(prime_counter *pc) {
}
else
prime_cache[prime_cache_idx++] = p;
}
pc->idx++;
if ((pc->exhausted = (p >= pc->stop)))
return 0;
Expand Down Expand Up @@ -180,14 +178,13 @@ uintmax_t advance_prime_sieve(prime_sieve *ps) {
uintmax_t step;
bool candidate_in_sieve = false;
size_t candidate_index = -1;
for (size_t i = 0; i < ps->sieve_len * 2; i += 2) {
for (size_t i = 0; i < ps->sieve_len * 2; i += 2)
if (ps->sieve[i] == ps->candidate) {
step = ps->sieve[i + 1];
candidate_in_sieve = true;
candidate_index = i;
break;
}
}
if (!candidate_in_sieve) {
if (ps->candidate < ps->prime_squared) { // prime
uintmax_t ret = ps->candidate;
Expand All @@ -204,12 +201,11 @@ uintmax_t advance_prime_sieve(prime_sieve *ps) {
do {
candidate += step;
candidate_in_sieve = false;
for (size_t i = 0; i < ps->sieve_len * 2; i += 2) {
for (size_t i = 0; i < ps->sieve_len * 2; i += 2)
if (ps->sieve[i] == candidate) {
candidate_in_sieve = true;
break;
}
}
} while (candidate_in_sieve);
if (candidate_index != -1)
ps->sieve[candidate_index] = candidate;
Expand Down
5 changes: 2 additions & 3 deletions c/src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ char *get_data_file(const char *name) {
return NULL;
}
#else
char* absolute_path = realpath(start, NULL);
char* absolute_path = realpath(start, NULL);
if (!absolute_path) {
perror("realpath");
return NULL;
Expand Down Expand Up @@ -90,12 +90,11 @@ char *get_data_file(const char *name) {
}

const size_t ret_code = fread(buffer, 1, length, file);
if (ret_code != length) {
if (ret_code != length)
if (feof(file))
printf("Error reading %s: unexpected end of file, read %" PRIu64 " of %"PRIu64" bytes expected\n", name, (uint64_t)ret_code, (uint64_t)length);
else if (ferror(file))
perror("Error reading data file");
}

buffer[length] = 0;
fclose(file);
Expand Down
7 changes: 1 addition & 6 deletions c/src/p0000_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0000() {
return 0;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0000());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0000)
#endif
7 changes: 1 addition & 6 deletions c/src/p0001.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0001() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0001());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0001)
#endif
7 changes: 1 addition & 6 deletions c/src/p0002.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0002() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0002());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0002)
#endif
7 changes: 1 addition & 6 deletions c/src/p0003.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,5 @@ uint16_t EMSCRIPTEN_KEEPALIVE p0003() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu16 "\n", p0003());
return 0;
}
#endif
PROGRAM_TAIL(PRIu16, p0003)
#endif
13 changes: 3 additions & 10 deletions c/src/p0004.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,22 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0004() {
uint32_t answer = 0, i, j, a, z, prod;
bool broken;
digit_counter dc;
for (i = 100; i < 1000; i++) {
for (i = 100; i < 1000; i++)
for (j = 100; j < 1000; j++) {
prod = i * j;
dc = digits(prod);
broken = false;
for (a = 0, z = dc.idx; a < z; a++, z--) {
for (a = 0, z = dc.idx; a < z; a++, z--)
if (dc.digits[a] != dc.digits[z]) {
broken = true;
break;
}
}
if (!broken)
answer = max(answer, prod);
free_digit_counter(dc);
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0004());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0004)
#endif
7 changes: 1 addition & 6 deletions c/src/p0005.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0005() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0005());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0005)
#endif
7 changes: 1 addition & 6 deletions c/src/p0006.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0006() {
return sum * sum - sum_of_squares;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0006());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0006)
#endif
7 changes: 1 addition & 6 deletions c/src/p0007.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,5 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0007() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0007());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0007)
#endif
7 changes: 1 addition & 6 deletions c/src/p0008.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0008() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0008());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0008)
#endif
10 changes: 2 additions & 8 deletions c/src/p0009.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,16 @@ Find the product abc.

uint32_t EMSCRIPTEN_KEEPALIVE p0009() {
uint32_t answer = 0;
for (uint32_t c = 3; !answer && c < 1000; c++) {
for (uint32_t c = 3; !answer && c < 1000; c++)
for (uint32_t b = 2; b < c; b++) {
uint32_t a = 1000 - c - b;
if (a < b && a*a + b*b == c*c) {
answer = a * b * c;
break;
}
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu32 "\n", p0009());
return 0;
}
#endif
PROGRAM_TAIL(PRIu32, p0009)
#endif
7 changes: 1 addition & 6 deletions c/src/p0010.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0010() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0010());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0010)
#endif
13 changes: 3 additions & 10 deletions c/src/p0011.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static const uint8_t grid[20][20] = {
uint64_t EMSCRIPTEN_KEEPALIVE p0011() {
uint64_t answer = 0, tmp;
uint8_t i, j;
for (i = 0; i < 20; i++) {
for (i = 0; i < 20; i++)
for (j = 0; j < 17; j++) {
// horizontal section
tmp = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3];
Expand All @@ -73,8 +73,7 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0011() {
tmp = grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i];
answer = max(answer, tmp);
}
}
for (i = 0; i < 17; i++) {
for (i = 0; i < 17; i++)
for (j = 0; j < 17; j++) {
// right diagonal section
tmp = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3];
Expand All @@ -83,14 +82,8 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0011() {
tmp = grid[i][j + 3] * grid[i + 1][j + 2] * grid[i + 2][j + 1] * grid[i + 3][j];
answer = max(answer, tmp);
}
}
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0011());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0011)
#endif
7 changes: 1 addition & 6 deletions c/src/p0012.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0012() {
return -1;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0012());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0012)
#endif
7 changes: 1 addition & 6 deletions c/src/p0013.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0013() {
return ret;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0013());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0013)
#endif
7 changes: 1 addition & 6 deletions c/src/p0014.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0014() {
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0014());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0014)
#endif
7 changes: 1 addition & 6 deletions c/src/p0015.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0015() {
return lattice_paths(20, 20);
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0015());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0015)
#endif
13 changes: 3 additions & 10 deletions c/src/p0016.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,10 @@ What is the sum of the digits of the number 21000?
uint64_t EMSCRIPTEN_KEEPALIVE p0016() {
uint64_t answer = 0;
BCD_int power = pow_cuint_cuint(256, 125);
for (size_t i = 0; i < power.bcd_digits; i++) {
answer += power.digits[i] & 0x0F;
answer += power.digits[i] >> 4;
}
for (size_t i = 0; i < power.bcd_digits; i++)
answer += (power.digits[i] & 0x0F) + (power.digits[i] >> 4);
return answer;
}

#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0016());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0016)
#endif
7 changes: 1 addition & 6 deletions c/src/p0017.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,5 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0017() {
}


#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu64 "\n", p0017());
return 0;
}
#endif
PROGRAM_TAIL(PRIu64, p0017)
#endif
Loading

0 comments on commit cc496ea

Please sign in to comment.