solved day 4 part 2 in reasonable runtime
This commit is contained in:
parent
6706d5a87e
commit
c32f519997
2 changed files with 154 additions and 0 deletions
141
4_2/main.c
Normal file
141
4_2/main.c
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
#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>
|
||||
|
||||
char input[] =
|
||||
{
|
||||
#embed "input.txt"
|
||||
,'\0' // null terminator
|
||||
,'\0'
|
||||
};
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
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 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)]);
|
||||
}else{
|
||||
printf("%c", input[(x)*(width+1)+(y)]);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
clock_t start, end;
|
||||
start = clock();
|
||||
|
||||
while(input[width] != '\n'){
|
||||
width++;
|
||||
}
|
||||
//printf("width: %d\n", width);
|
||||
|
||||
while(input[height*(width+1)+1] != 0){
|
||||
height++;
|
||||
}
|
||||
//printf("height: %d\n", height);
|
||||
|
||||
|
||||
uint8_t* surrounds = calloc((height+2)*(width+2), sizeof(uint8_t))+width+1;
|
||||
|
||||
for(int x = 0; x < height; x++){
|
||||
for(int y = 0; y< width; y++){
|
||||
if(input[(x)*(width+1)+(y)] == '@'){
|
||||
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;
|
||||
}
|
||||
//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)]);
|
||||
}
|
||||
}
|
||||
//printf("\n");
|
||||
}
|
||||
//print_field(surrounds);
|
||||
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(surrounds-width-1);
|
||||
|
||||
end = clock();
|
||||
printf("count: %lu\n", count);
|
||||
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