D3 part 1
This commit is contained in:
47
2023/d3/d3.c
Normal file
47
2023/d3/d3.c
Normal file
@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* #define WIDTH 11 */
|
||||
/* #define HEIGHT 10 */
|
||||
/* #define file "test.txt" */
|
||||
|
||||
/* #define WIDTH 21 */
|
||||
/* #define HEIGHT 20 */
|
||||
/* #define file "test2.txt" */
|
||||
|
||||
#define WIDTH 141
|
||||
#define HEIGHT 140
|
||||
#define file "input.txt"
|
||||
|
||||
int main()
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
FILE *fd = fopen(file, "r");
|
||||
char *cnt = malloc(sizeof(cnt) * WIDTH * HEIGHT);
|
||||
fread(cnt, 1, WIDTH * HEIGHT, fd);
|
||||
|
||||
char num_c[4] = {0};
|
||||
for (int i = 0; i < WIDTH*HEIGHT; i++) {
|
||||
int pos = 0;
|
||||
bool take = false;
|
||||
if(i % WIDTH != 0 && (cnt[i-1] != '.' || (i / WIDTH != 0 && cnt[i-1-WIDTH] != '.') || (i / WIDTH != HEIGHT-1 && cnt[i-1+WIDTH] != '.')))
|
||||
take = true;
|
||||
while(cnt[i] >= '0' && cnt[i] <= '9'){
|
||||
if((i/WIDTH != 0 && cnt[i - WIDTH] != '.') || ( i / WIDTH != HEIGHT-1 && cnt[i + WIDTH] != '.'))
|
||||
take = true;
|
||||
num_c[pos++] = cnt[i++];
|
||||
}
|
||||
if(i % WIDTH != (WIDTH-1) && (cnt[i] != '.' || ((i-1) / WIDTH != 0 && cnt[i-WIDTH] != '.') || ((i-1) / WIDTH != (HEIGHT-1) && cnt[i+WIDTH] != '.')))
|
||||
take = true;
|
||||
num_c[pos] = 0;
|
||||
if(pos > 0 && take){
|
||||
sum += atoi(num_c);
|
||||
printf("%d\n", atoi(num_c));
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user