diff options
Diffstat (limited to 'Advent-of-Code-2022/aoc-8/main.c')
-rw-r--r-- | Advent-of-Code-2022/aoc-8/main.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-8/main.c b/Advent-of-Code-2022/aoc-8/main.c new file mode 100644 index 0000000..ee36c26 --- /dev/null +++ b/Advent-of-Code-2022/aoc-8/main.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#if 0 + #define PART part1 +#else + #define PART part2 +#endif + +#if 0 + #define FILENAME "sample.txt" + #define SIDE 5 +#else + #define FILENAME "input.txt" + #define SIDE 99 +#endif + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) + +int ans = 0; +int table[SIDE][SIDE]; + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not opne file: %s\n", FILENAME); + exit(1); + } + + char line[256]; + int col = 0; + while(fgets(line, sizeof(line), fp)) + { + assert(col < SIDE); + + for(int i = 0; i < SIDE; i++) + table[i][col] = line[i] - '0'; + col++; + } + + fclose(fp); +} + +void part1() +{ + ans = (SIDE*4) - 4; + + for(int y = 1; y < (SIDE-1); y++) + for(int x = 1; x < (SIDE-1); x++) + { + int cur = table[x][y]; + + // row left + int rl = 1; + for(int i = 0; i < x; i++) + rl &= cur > table[i][y]; + + // row right + int rr = 1; + for(int i = (x+1); i < SIDE; i++) + rr &= cur > table[i][y]; + + // col up + int cu = 1; + for(int i = 0; i < y; i++) + cu &= cur > table[x][i]; + + // col down + int cd = 1; + for(int i = (y+1); i < SIDE; i++) + cd &= cur > table[x][i]; + + if(rl || rr || cu || cd) ans++; + } +} + +void part2() +{ + for(int y = 1; y < (SIDE-1); y++) + for(int x = 1; x < (SIDE-1); x++) + { + int cur = table[x][y]; + + // row left + int rl = 0; + for(int i = (x-1); i >= 0; i--) { + rl++; + if(cur <= table[i][y]) break; + } + + // row right + int rr = 0; + for(int i = (x+1); i < SIDE; i++) { + rr++; + if(cur <= table[i][y]) break; + } + + // col up + int cu = 0; + for(int i = (y-1); i >= 0; i--) { + cu++; + if(cur <= table[x][i]) break; + } + + // col down + int cd = 0; + for(int i = (y+1); i < SIDE; i++) { + cd++; + if(cur <= table[x][i]) break; + } + + ans = MAX(ans, rl * rr * cu * cd); + } +} + +int main(void) +{ + parse(); + PART(); + printf("%d\n", ans); + return 0; +} |