Skip to content

Commit

Permalink
Attempt to compress code
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 11, 2024
1 parent f3aa2c0 commit 6e8fc69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
4 changes: 1 addition & 3 deletions c/src/p0015.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ How many such routes are there through a 20×20 grid?
#include "include/macros.h"
#include "include/math.h"

#define lattice_paths(height, width) (n_choose_r(height + width, height))

uint64_t EMSCRIPTEN_KEEPALIVE p0015() {
return lattice_paths(20, 20);
return n_choose_r(40, 20);
}

PROGRAM_TAIL("%" PRIu64, p0015)
Expand Down
26 changes: 11 additions & 15 deletions cplusplus/src/p0013.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,23 +217,19 @@ static const uint64_t ten18 = 1000000000000000000;
static const uint64_t ten10 = 10000000000;

uint64_t EMSCRIPTEN_KEEPALIVE p0013() {
uint64_t high = 0, med = 0, low = 0;
uint64_t arr[3] = { 0 };
for (uint8_t i = 0; i < 100; ++i) {
low += numbers[i][2];
med += numbers[i][1];
high += numbers[i][0];
if (low > ten18) {
med += low / ten18;
low %= ten18;
}
if (med > ten18) {
high += med / ten18;
med %= ten18;
}
for (uint8_t j = 0; j < 3; ++j)
arr[j] += numbers[i][j];
for (uint8_t j = 2; j > 0; --j)
if (arr[j] > ten18) {
arr[j - 1] += arr[j] / ten18;
arr[j] %= ten18;
}
}
while (high > ten10)
high /= 10;
return high;
while (arr[0] > ten10)
arr[0] /= 10;
return arr[0];
}

PROGRAM_TAIL(p0013)
Expand Down
4 changes: 1 addition & 3 deletions cplusplus/src/p0015.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ How many such routes are there through a 20×20 grid?
#include "include/macros.hpp"
#include "include/math.hpp"

#define lattice_paths(height, width) (n_choose_r(height + width, height))

uint64_t EMSCRIPTEN_KEEPALIVE p0015() {
return lattice_paths(20, 20);
return n_choose_r(40, 20);
}

PROGRAM_TAIL(p0015)
Expand Down
16 changes: 6 additions & 10 deletions cplusplus/src/p0017.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,14 @@ std::string ToString(uint64_t n) {

uint64_t EMSCRIPTEN_KEEPALIVE p0017() {
uint64_t answer = 0;
std::string filter[2] = {" ", "-"};
size_t pos, i;
for (uint32_t x = 1; x < 1001; x += 1) {
std::string str = ToString(x);
size_t pos = str.find(" ");
while (pos != std::string::npos) {
str.replace(pos, 1, "");
pos = str.find(" ", pos + 1);
}
pos = str.find("-");
while (pos != std::string::npos) {
str.replace(pos, 1, "");
pos = str.find("-", pos + 1);
}
for (pos = 0, i = 0; i < 2; i++)
while ((pos = str.find(" ", pos)) != std::string::npos)
str.replace(pos, 1, "");

answer += str.length();
}
return answer;
Expand Down

0 comments on commit 6e8fc69

Please sign in to comment.