Compare commits

...

10 commits

10 changed files with 760 additions and 53 deletions

View file

@ -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);

View file

@ -7,6 +7,10 @@
#include <unistd.h>
#include <time.h>
#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();

13
5/Makefile Normal file
View file

@ -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

218
5/main.c Normal file
View file

@ -0,0 +1,218 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
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; i<len-1; i++){
int curr_min = i;
for(int o = i+1; o<len; o++){
if(starts[curr_min]>starts[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] && storage<starts[guess+1]){
// break;
// }else if(storage>starts[guess]){
// guess += diff;
// }else{
// guess -= diff;
// }
// }
// if(storage<ends[guess]){
// return true;
// }else{
// return false;
// }
//}
bool check_fresh(uint64_t storage, uint64_t* starts, uint64_t* ends, int length){
//printf("checking %lu", storage);
for(int i = 0; i<length; i++){
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<ends[i+n]){
end = ends[i+n];
}
}
starts[new_count]=starts[i];
ends[new_count]=end;
new_count++;
i+=n;
}
starts[new_count+1] = ULLONG_MAX;
*range_count = new_count;
}
int main(int argc, char *argv[]){
clock_t start, end;
start = clock();
//parse ranges first
int i = 0;
int range_count = 0;
while(input[i] != '\n'){
while(input[i] != '\n'){
i++;
}
range_count++;
i++;
}
//printf("range count: %d\n", range_count);
i++;
int storage_count = 0;
while(input[i] != 0){
while(input[i] != '\n'){
i++;
}
storage_count++;
i++;
}
//printf("Storage count: %d\n", storage_count);
uint64_t starts [range_count+1];
uint64_t ends [range_count+1];
i = 0;
int range_num = 0;
int len = 0;
while(input[i] != '\n'){
starts[range_num] = parseNum(&input[i], &len);
i+=len+1;
ends[range_num] = parseNum(&input[i], &len);
i+=len+1;
//printf("%d-%d\n", starts[range_num], ends[range_num]);
range_num++;
}
//for(int i = 0; i<range_count; i++){
// printf("%lu-%lu\n", starts[i], ends[i]);
//}
range_num = 0;
uint64_t* storage = malloc(sizeof(uint64_t)*(storage_count));
i++;
while(input[i] != 0){
storage[range_num] = parseNum(&input[i], &len);
i+=len+1;
//printf("%d-%d\n", starts[range_num], ends[range_num]);
range_num++;
}
//printf("\n");
//for(int i = 0; i<storage_count; i++){
// printf("%lu\n", storage[i]);
//}
sort_ranges(starts, ends, range_count);
//join_ranges(starts, ends, &range_count);
//for(int i = 0; i<range_count; i++){
// printf("%lu-%lu\n", starts[i], ends[i]);
//}
for(int i = 0; i<storage_count; i++){
if(check_fresh(storage[i], starts, ends, range_count)){
result++;
}
}
end = clock();
printf("count: %lu\n", result);
clock_t ticks_taken = end - start;
double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC;
printf("time take: %f ticks\n", time_taken);
printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
exit(1);
}

13
5_2/Makefile Normal file
View file

@ -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

216
5_2/main.c Normal file
View file

@ -0,0 +1,216 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
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; i<len-1; i++){
int curr_min = i;
for(int o = i+1; o<len; o++){
if(starts[curr_min]>starts[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] && storage<starts[guess+1]){
// break;
// }else if(storage>starts[guess]){
// guess += diff;
// }else{
// guess -= diff;
// }
// }
// if(storage<ends[guess]){
// return true;
// }else{
// return false;
// }
//}
bool check_fresh(uint64_t storage, uint64_t* starts, uint64_t* ends, int length){
//printf("checking %lu", storage);
for(int i = 0; i<length; i++){
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<ends[i+n]){
end = ends[i+n];
}
}
starts[new_count]=starts[i];
ends[new_count]=end;
new_count++;
i+=n;
}
starts[new_count+1] = ULLONG_MAX;
*range_count = new_count;
}
int main(int argc, char *argv[]){
clock_t start, end;
start = clock();
//parse ranges first
int i = 0;
int range_count = 0;
while(input[i] != '\n'){
while(input[i] != '\n'){
i++;
}
range_count++;
i++;
}
//printf("range count: %d\n", range_count);
//i++;
//int storage_count = 0;
//while(input[i] != 0){
// while(input[i] != '\n'){
// i++;
// }
// storage_count++;
// i++;
//}
//printf("Storage count: %d\n", storage_count);
uint64_t starts [range_count+1];
uint64_t ends [range_count+1];
i = 0;
int range_num = 0;
int len = 0;
while(input[i] != '\n'){
starts[range_num] = parseNum(&input[i], &len);
i+=len+1;
ends[range_num] = parseNum(&input[i], &len);
i+=len+1;
//printf("%d-%d\n", starts[range_num], ends[range_num]);
range_num++;
}
//for(int i = 0; i<range_count; i++){
// printf("%lu-%lu\n", starts[i], ends[i]);
//}
//range_num = 0;
//uint64_t* storage = malloc(sizeof(uint64_t)*(storage_count));
//i++;
//while(input[i] != 0){
// storage[range_num] = parseNum(&input[i], &len);
// i+=len+1;
//
// //printf("%d-%d\n", starts[range_num], ends[range_num]);
// range_num++;
//}
//printf("\n");
//for(int i = 0; i<storage_count; i++){
// printf("%lu\n", storage[i]);
//}
sort_ranges(starts, ends, range_count);
join_ranges(starts, ends, &range_count);
//for(int i = 0; i<range_count; i++){
// printf("%lu-%lu\n", starts[i], ends[i]);
//}
for(int i = 0; i<range_count; i++){
result += ends[i]+1-starts[i];
}
end = clock();
printf("count: %lu\n", result);
clock_t ticks_taken = end - start;
double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC;
printf("time take: %f ticks\n", time_taken);
printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
exit(1);
}

13
6/Makefile Normal file
View file

@ -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

105
6/main.c Normal file
View file

@ -0,0 +1,105 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
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<lines; i++){
uint64_t num = parseNum(symbols-(i*(width+1)));
//printf("parsed %lu\n", num);
col_res *= num;
}
}else if(*symbols == '+'){
col_res = 0;
//printf("adding....\n");
for(int i = 1; i<lines; i++){
uint64_t num = parseNum(symbols-(i*(width+1)));
//printf("parsed %lu\n", num);
col_res += num;
}
}
//printf("total: %lu\n", col_res);
result += col_res;
symbols += parse_length+1;
parse_length = 1;
}
end = clock();
printf("count: %lu\n", result);
clock_t ticks_taken = end - start;
double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC;
printf("time take: %f ticks\n", time_taken);
printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
exit(1);
}

13
6_2/Makefile Normal file
View file

@ -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

106
6_2/main.c Normal file
View file

@ -0,0 +1,106 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
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)<lines && str[i] != ' '){
//printf("%c", str[i]);
out += str[i]-0x30;
out *= 10;
i+=len;
}
out /=10;
//printf("\t%lu\n", out);
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);
}lines--;
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", parseNumVert(input, width+1, lines));
int parse_length = 1;
uint64_t col_res = 0;;
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 = 0; i<parse_length; i++){
uint64_t num = parseNumVert(symbols-(lines*(width+1))+i, width+1, lines);
//printf("parsed %lu\n", num);
col_res *= num;
}
}else if(*symbols == '+'){
col_res = 0;
//printf("adding....\n");
for(int i = 0; i<parse_length; i++){
uint64_t num = parseNumVert(symbols-(lines*(width+1))+i, width+1, lines);
//printf("parsed %lu\n", num);
col_res += num;
}
}
//printf("total: %lu\n", col_res);
result += col_res;
symbols += parse_length+1;
parse_length = 1;
}
end = clock();
printf("count: %lu\n", result);
clock_t ticks_taken = end - start;
double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC;
printf("time take: %f ticks\n", time_taken);
printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
exit(1);
}