diff options
| author | Ian C <ianc@noddybox.co.uk> | 2021-12-09 23:08:40 +0000 |
|---|---|---|
| committer | Ian C <ianc@noddybox.co.uk> | 2021-12-09 23:08:40 +0000 |
| commit | 1dfae1e03aee84b73d0114dfa16d827373ea6ce7 (patch) | |
| tree | 56dd316e1c052e2f10067e82d6e68b5341070a93 /9.c | |
| parent | 466d739f6092eaf0c7f0c5646b19ed65959d268b (diff) | |
Added day 9
Diffstat (limited to '9.c')
| -rw-r--r-- | 9.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -0,0 +1,72 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define MAX_SIZE 256 + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static int Lowest(char map[MAX_SIZE][MAX_SIZE], + int x, int y, int width, int height) +{ + char val; + + val = map[y][x]; + + return (y == 0 || map[y-1][x] > val) && + (y == height - 1 || map[y+1][x] > val) && + (x ==0 || map[y][x-1] > val) && + (x == width -1 || map[y][x+1] > val); +} + +int main(void) +{ + char buff[0x8000]; + char map[MAX_SIZE][MAX_SIZE]={0}; + int map_height = 0; + int map_width = 0; + int sum = 0; + int x = 0; + int y = 0; + int f = 0; + + while(ReadLine(buff, sizeof buff, stdin)) + { + map_width = strlen(buff); + + for(f = 0; f < strlen(buff); f++) + { + map[map_height][f] = buff[f] - '0'; + } + + map_height++; + } + + for(y = 0; y < map_height; y++) + { + for(x = 0; x < map_width; x++) + { + if (Lowest(map, x, y, map_width, map_height)) + { + sum += map[y][x] + 1; + } + } + } + + printf("sum = %d\n", sum); + + return 0; +} |
