Compare commits
No commits in common. "f5de12b56890de9acee702e76183d836b5559875" and "c32f519997f895f56f06237d43ba96bbb83234dc" have entirely different histories.
f5de12b568
...
c32f519997
10 changed files with 53 additions and 760 deletions
2
2/main.c
2
2/main.c
|
|
@ -49,7 +49,7 @@ uint64_t power(uint64_t base, uint64_t exp) {
|
||||||
for (i = 0; i < exp; i++)
|
for (i = 0; i < exp; i++)
|
||||||
result *= base;
|
result *= base;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool check_silly(uint64_t in){
|
bool check_silly(uint64_t in){
|
||||||
int num = numPlaces(in);
|
int num = numPlaces(in);
|
||||||
|
|
|
||||||
88
4_2/main.c
88
4_2/main.c
|
|
@ -7,10 +7,6 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define ANSI_COLOR_RED "\x1b[31m"
|
|
||||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
|
||||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
|
||||||
|
|
||||||
char input[] =
|
char input[] =
|
||||||
{
|
{
|
||||||
#embed "input.txt"
|
#embed "input.txt"
|
||||||
|
|
@ -32,16 +28,14 @@ uint64_t power(uint64_t base, uint64_t exp) {
|
||||||
void print_field(uint8_t* surrounds){
|
void print_field(uint8_t* surrounds){
|
||||||
for(int x = 0; x < height; x++){
|
for(int x = 0; x < height; x++){
|
||||||
for(int y = 0; y < width; y++){
|
for(int y = 0; y < width; y++){
|
||||||
if((surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){
|
if(/*surrounds[(x)*(width+2)+(y)] < 4 &&*/ input[(x)*(width+1)+(y)] == '@'){
|
||||||
printf(ANSI_COLOR_GREEN"%d\t", (surrounds[(x)*(width+2)+(y)]-0x80));
|
printf("%d", surrounds[(x)*(width+2)+(y)]);
|
||||||
}else{
|
}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(ANSI_COLOR_RESET "\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("==============================================\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
|
|
@ -68,75 +62,71 @@ int main(int argc, char *argv[]){
|
||||||
surrounds[(x-1)*(width+2)+(y)] += 1;
|
surrounds[(x-1)*(width+2)+(y)] += 1;
|
||||||
surrounds[(x-1)*(width+2)+(y+1)] += 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)*(width+2)+(y)] += 0x80;
|
surrounds[(x)*(width+2)+(y)] += 0;
|
||||||
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)] += 1;
|
||||||
surrounds[(x+1)*(width+2)+(y)] += 1;
|
surrounds[(x+1)*(width+2)+(y)] += 1;
|
||||||
surrounds[(x+1)*(width+2)+(y+1)] += 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 = 0;
|
||||||
uint64_t count_2 = 0;
|
uint64_t count_2 = 0;
|
||||||
bool cont = true;
|
bool cont = true;
|
||||||
int* removes = calloc((height)*(width)*2, sizeof(int));
|
while (cont){
|
||||||
int* new_removes = calloc((height)*(width)*2, sizeof(int));
|
|
||||||
int new_count_2 = 0;
|
|
||||||
count_2 = 0;
|
count_2 = 0;
|
||||||
|
|
||||||
for(int x = 0; x < height; x++){
|
for(int x = 0; x < height; x++){
|
||||||
for(int y = 0; y < width; y++){
|
for(int y = 0; y < width; y++){
|
||||||
if((surrounds[(x)*(width+2)+(y)]&0x7f) < 4 && (surrounds[(x)*(width+2)+(y)]&0x80) == 0x80){
|
if(surrounds[(x)*(width+2)+(y)] < 4 && input[(x)*(width+1)+(y)] == '@'){
|
||||||
|
|
||||||
removes[count_2*2] = x;
|
|
||||||
removes[count_2*2+1] = y;
|
|
||||||
count_2++;
|
count_2++;
|
||||||
surrounds[(x)*(width+2)+(y)] -= 0x80;
|
input[(x)*(width+1)+(y)] = 'x';
|
||||||
|
//printf("%d", surrounds[(x)*width+(y)]);
|
||||||
|
}else{
|
||||||
|
//printf("%c", input[(x)*width+(y)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//printf("\n");
|
||||||
}
|
}
|
||||||
|
//print_field(surrounds);
|
||||||
count += count_2;
|
count += count_2;
|
||||||
|
if(count_2 == 0){
|
||||||
|
cont = false;
|
||||||
while (count_2 != 0){
|
}
|
||||||
for(int i = 0; i < count_2; i++){
|
//printf("cont: %s\n", cont ? "true" : "false");
|
||||||
int x = removes[i*2];
|
//printf("count_2 = %lu\n", count_2);
|
||||||
int y = removes[i*2+1];
|
//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)] -= 1;
|
||||||
surrounds[(x-1)*(width+2)+(y)] -= 1;
|
surrounds[(x-1)*(width+2)+(y)] -= 1;
|
||||||
surrounds[(x-1)*(width+2)+(y+1)] -= 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)*(width+2)+(y)] -= 0;
|
||||||
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)] -= 1;
|
||||||
surrounds[(x+1)*(width+2)+(y)] -= 1;
|
surrounds[(x+1)*(width+2)+(y)] -= 1;
|
||||||
surrounds[(x+1)*(width+2)+(y+1)] -= 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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++;
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(removes, new_removes, new_count_2*2*sizeof(int));
|
|
||||||
count_2 = new_count_2;
|
|
||||||
count += count_2;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(removes);
|
|
||||||
free(surrounds-width-1);
|
free(surrounds-width-1);
|
||||||
|
|
||||||
end = clock();
|
end = clock();
|
||||||
|
|
|
||||||
13
5/Makefile
13
5/Makefile
|
|
@ -1,13 +0,0 @@
|
||||||
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
218
5/main.c
|
|
@ -1,218 +0,0 @@
|
||||||
#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
13
5_2/Makefile
|
|
@ -1,13 +0,0 @@
|
||||||
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
216
5_2/main.c
|
|
@ -1,216 +0,0 @@
|
||||||
#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
13
6/Makefile
|
|
@ -1,13 +0,0 @@
|
||||||
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
105
6/main.c
|
|
@ -1,105 +0,0 @@
|
||||||
#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
13
6_2/Makefile
|
|
@ -1,13 +0,0 @@
|
||||||
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
106
6_2/main.c
|
|
@ -1,106 +0,0 @@
|
||||||
#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);
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue