diff --git a/2/main.c b/2/main.c index d45a6ef..3499bd1 100644 --- a/2/main.c +++ b/2/main.c @@ -49,7 +49,7 @@ uint64_t power(uint64_t base, uint64_t exp) { for (i = 0; i < exp; i++) result *= base; return result; - } +} bool check_silly(uint64_t in){ int num = numPlaces(in); diff --git a/4_2/main.c b/4_2/main.c index ba9f621..757e050 100644 --- a/4_2/main.c +++ b/4_2/main.c @@ -7,6 +7,10 @@ #include #include +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_RESET "\x1b[0m" + char input[] = { #embed "input.txt" @@ -28,14 +32,16 @@ uint64_t power(uint64_t base, uint64_t exp) { void print_field(uint8_t* surrounds){ for(int x = 0; x < height; x++){ for(int y = 0; y < width; y++){ - if(/*surrounds[(x)*(width+2)+(y)] < 4 &&*/ input[(x)*(width+1)+(y)] == '@'){ - printf("%d", surrounds[(x)*(width+2)+(y)]); + if((surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){ + printf(ANSI_COLOR_GREEN"%d\t", (surrounds[(x)*(width+2)+(y)]-0x80)); }else{ - printf("%c", input[(x)*(width+1)+(y)]); + //printf("%c", input[(x)*(width+1)+(y)]); + printf(ANSI_COLOR_RED "%d\t", surrounds[(x)*(width+2)+(y)]); } } - printf("\n"); + printf(ANSI_COLOR_RESET "\n"); } + printf("==============================================\n"); } int main(int argc, char *argv[]){ @@ -62,71 +68,75 @@ int main(int argc, char *argv[]){ surrounds[(x-1)*(width+2)+(y)] += 1; surrounds[(x-1)*(width+2)+(y+1)] += 1; surrounds[(x)*(width+2)+(y-1)] += 1; - surrounds[(x)*(width+2)+(y)] += 0; + surrounds[(x)*(width+2)+(y)] += 0x80; surrounds[(x)*(width+2)+(y+1)] += 1; surrounds[(x+1)*(width+2)+(y-1)] += 1; surrounds[(x+1)*(width+2)+(y)] += 1; surrounds[(x+1)*(width+2)+(y+1)] += 1; } - //if(surrounds[(x)*width+(y)] < 8 && input[(x)*(width+1)+(y)] == '@'){ - // printf("%d", surrounds[(x)*(width+2)+(y)]); - //}else{ - // printf("%c", input[(x)*(width+1)+(y)]); - //} } - //printf("\n"); } uint64_t count = 0; uint64_t count_2 = 0; bool cont = true; - while (cont){ - count_2 = 0; - for(int x = 0; x < height; x++){ - for(int y = 0; y < width; y++){ - if(surrounds[(x)*(width+2)+(y)] < 4 && input[(x)*(width+1)+(y)] == '@'){ - count_2++; - input[(x)*(width+1)+(y)] = 'x'; - //printf("%d", surrounds[(x)*width+(y)]); - }else{ - //printf("%c", input[(x)*width+(y)]); + int* removes = calloc((height)*(width)*2, sizeof(int)); + int* new_removes = calloc((height)*(width)*2, sizeof(int)); + int new_count_2 = 0; + count_2 = 0; + + for(int x = 0; x < height; x++){ + for(int y = 0; y < width; y++){ + if((surrounds[(x)*(width+2)+(y)]&0x7f) < 4 && (surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){ + + removes[count_2*2] = x; + removes[count_2*2+1] = y; + count_2++; + surrounds[(x)*(width+2)+(y)] -= 0x80; + } + } + } + count += count_2; + + + while (count_2 != 0){ + for(int i = 0; i < count_2; i++){ + int x = removes[i*2]; + int y = removes[i*2+1]; + surrounds[(x-1)*(width+2)+(y-1)] -= 1; + surrounds[(x-1)*(width+2)+(y)] -= 1; + surrounds[(x-1)*(width+2)+(y+1)] -= 1; + surrounds[(x)*(width+2)+(y-1)] -= 1; + surrounds[(x)*(width+2)+(y+1)] -= 1; + surrounds[(x+1)*(width+2)+(y-1)] -= 1; + surrounds[(x+1)*(width+2)+(y)] -= 1; + surrounds[(x+1)*(width+2)+(y+1)] -= 1; + } + + new_count_2 = 0; + for(int i = 0; i < count_2; i++){ + int x = removes[i*2]; + int y = removes[i*2+1]; + for(int xd = -1; xd<=1; xd++){ + for(int yd = -1; yd<=1; yd++){ + if((surrounds[(x+xd)*(width+2)+(y+yd)]) < 132 && (surrounds[(x+xd)*(width+2)+(y+yd)]) >= 128){ + new_removes[new_count_2*2] = x+xd; + new_removes[new_count_2*2+1] = y+yd; + + surrounds[(x+xd)*(width+2)+(y+yd)] -= 0x80; + new_count_2++; + //} + } } } - //printf("\n"); } - //print_field(surrounds); + + memcpy(removes, new_removes, new_count_2*2*sizeof(int)); + count_2 = new_count_2; count += count_2; - if(count_2 == 0){ - cont = false; - } - //printf("cont: %s\n", cont ? "true" : "false"); - //printf("count_2 = %lu\n", count_2); - //printf("count = %lu\n", count); - for(int x = 0; x < height; x++){ - for(int y = 0; y < width; y++){ - if(input[(x)*(width+1)+(y)] == 'x'){ - surrounds[(x-1)*(width+2)+(y-1)] -= 1; - surrounds[(x-1)*(width+2)+(y)] -= 1; - surrounds[(x-1)*(width+2)+(y+1)] -= 1; - surrounds[(x)*(width+2)+(y-1)] -= 1; - surrounds[(x)*(width+2)+(y)] -= 0; - surrounds[(x)*(width+2)+(y+1)] -= 1; - surrounds[(x+1)*(width+2)+(y-1)] -= 1; - surrounds[(x+1)*(width+2)+(y)] -= 1; - surrounds[(x+1)*(width+2)+(y+1)] -= 1; - input[(x)*(width+1)+(y)] = '.'; - //printf("%d", surrounds[(x)*width+(y)]); - }else{ - //printf("%c", input[(x)*width+(y)]); - } - } - //printf("\n"); - } - //print_field(surrounds); - //sleep(1); } - + free(removes); free(surrounds-width-1); end = clock(); diff --git a/5/Makefile b/5/Makefile new file mode 100644 index 0000000..ba44bd3 --- /dev/null +++ b/5/Makefile @@ -0,0 +1,13 @@ +client: main.c + gcc -o main main.c -O3 -Wall -Wextra -g -march=x86-64-v3 + +client-debug: main.c + gcc -o main main.c -O1 -Wall -Wextra -g -fsanitize=address + +client-debug-nosanitize: main.c + gcc -o main main.c -O1 -Wall -Wextra -g + +clean: + rm main + +.PHONY: clean \ No newline at end of file diff --git a/5/main.c b/5/main.c new file mode 100644 index 0000000..a0e5031 --- /dev/null +++ b/5/main.c @@ -0,0 +1,218 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char input[] = +{ +#embed "input.txt" +,'\0' // null terminator +}; + +int width = 0; +int height = 0; +uint64_t result = 0; + + +uint64_t parseNum(char* str, int* len){ + int i = 0; + uint64_t out = 0; + while(str[i] != ',' && str[i] != '-' && str[i] != '\n'){ + out += str[i]-0x30; + out *= 10; + i++; + } + out /=10; + *len = i; + return out; +} + +int numPlaces (uint64_t n) { + int r = 1; + while (n > 9) { + n /= 10; + r++; + } + //printf("%d has %d places\n", n, r); + return r; +} + +uint64_t power(uint64_t base, uint64_t exp) { + uint64_t i, result = 1; + for (i = 0; i < exp; i++) + result *= base; + return result; +} + +void sort_ranges(uint64_t* starts, uint64_t* ends, int len){ + + for(int i = 0; istarts[o]){ + //printf("%lu is bigger than %lu, so we have a new lowest\n", starts[curr_min], starts[o]); + curr_min = o; + } + } + uint64_t tmp_start = starts[curr_min]; + uint64_t tmp_end = ends[curr_min]; + + starts[curr_min] = starts[i]; + ends[curr_min] = ends[i]; + + starts[i] = tmp_start; + ends[i] = tmp_end; + } +} + +//bool check_fresh(uint64_t storage, uint64_t* starts, uint64_t* ends, int length){ +// int guess = length/2; +// int diff = length/4; +// while(true){ +// printf("guess is %d", guess); +// diff = (diff/2)+1; +// if(storage>starts[guess] && storagestarts[guess]){ +// guess += diff; +// }else{ +// guess -= diff; +// } +// } +// if(storage=starts[i] && storage<=ends[i]){ + //printf(" .....fresh \t(is in range %lu - %lu)\n", starts[i], ends[i]); + return true; + } + } + + //printf(" .....spoiled\n"); + return false; +} + +void join_ranges(uint64_t* starts, uint64_t* ends, int* range_count){ + int new_count = 0; + for(int i = 0; i<*range_count; i++){ + int n=0; + uint64_t end = ends[i]; + while(starts[i+n+1]<=end+1){ + n++; + if(end +#include +#include +#include +#include +#include +#include +#include +#include + +char input[] = +{ +#embed "input.txt" +,'\0' // null terminator +}; + +int width = 0; +int height = 0; +uint64_t result = 0; + + +uint64_t parseNum(char* str, int* len){ + int i = 0; + uint64_t out = 0; + while(str[i] != ',' && str[i] != '-' && str[i] != '\n'){ + out += str[i]-0x30; + out *= 10; + i++; + } + out /=10; + *len = i; + return out; +} + +int numPlaces (uint64_t n) { + int r = 1; + while (n > 9) { + n /= 10; + r++; + } + //printf("%d has %d places\n", n, r); + return r; +} + +uint64_t power(uint64_t base, uint64_t exp) { + uint64_t i, result = 1; + for (i = 0; i < exp; i++) + result *= base; + return result; +} + +void sort_ranges(uint64_t* starts, uint64_t* ends, int len){ + + for(int i = 0; istarts[o]){ + //printf("%lu is bigger than %lu, so we have a new lowest\n", starts[curr_min], starts[o]); + curr_min = o; + } + } + uint64_t tmp_start = starts[curr_min]; + uint64_t tmp_end = ends[curr_min]; + + starts[curr_min] = starts[i]; + ends[curr_min] = ends[i]; + + starts[i] = tmp_start; + ends[i] = tmp_end; + } +} + +//bool check_fresh(uint64_t storage, uint64_t* starts, uint64_t* ends, int length){ +// int guess = length/2; +// int diff = length/4; +// while(true){ +// printf("guess is %d", guess); +// diff = (diff/2)+1; +// if(storage>starts[guess] && storagestarts[guess]){ +// guess += diff; +// }else{ +// guess -= diff; +// } +// } +// if(storage=starts[i] && storage<=ends[i]){ + //printf(" .....fresh \t(is in range %lu - %lu)\n", starts[i], ends[i]); + return true; + } + } + + //printf(" .....spoiled\n"); + return false; +} + +void join_ranges(uint64_t* starts, uint64_t* ends, int* range_count){ + int new_count = 0; + for(int i = 0; i<*range_count; i++){ + int n=0; + uint64_t end = ends[i]; + while(starts[i+n+1]<=end+1){ + n++; + if(end +#include +#include +#include +#include +#include +#include +#include +#include + +char input[] = +{ +#embed "input.txt" +,'\0' // null terminator +}; + +int width = 0; +int height = 0; +uint64_t result = 0; + + +uint64_t parseNum(char* str){ + int i = 0; + while(str[i] == ' '){ + i++; + } + uint64_t out = 0; + while(str[i] != ' ' && str[i] != '\n'){ + //printf("%c", str[i]); + out += str[i]-0x30; + out *= 10; + i++; + } + out /=10; + return out; +} + +int main(int argc, char *argv[]){ + clock_t start, end; + start = clock(); + + + while(input[width] != '\n'){ + width++; + } + + //printf("width: %d\n", width); + //parse ranges first + int i = 0; + int lines = 0; + while(input[i+1] != 0){ + lines++; + i+=(width+1); + } + + + char* symbols = input+(width+1)*(lines-1); + //printf("line count: %d\n", lines); + //printf("symbols:\n%s\n", symbols); + + //printf("parsing the first number: %lu\n", parseNum(input+8)); + + + int parse_length = 1; + uint64_t col_res; + while(*symbols != 0){ + while(symbols[parse_length] == ' ' || symbols[parse_length] == '\n'){ + parse_length++; + } + parse_length--; + //printf("parsing %d long numbers\n", parse_length); + + if(*symbols == '*'){ + col_res = 1; + //printf("multiplying....\n"); + for(int i = 1; i +#include +#include +#include +#include +#include +#include +#include +#include + +char input[] = +{ +#embed "input.txt" +,'\0' // null terminator +}; + +int width = 0; +int height = 0; +uint64_t result = 0; + + +uint64_t parseNumVert(char* str, int len, int lines){ + int i = 0; + while(str[i] == ' '){ + i+=len; + } + uint64_t out = 0; + while((i/len)