Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

#include <stdio.h>

#define SIZE 99

int verify(int trees[SIZE][SIZE],int i, int j, int *ss){
	int v1 = 1,v2=1,v3=1,v4=1;
	int q1 = 1,q2=1,q3=1,q4=1;
	int k;
	for(k=1; j+k < SIZE && v1 != 0; k++)
		v1 = v1 && (trees[i][j] > trees[i][j+k]);
	q1=k-1;
	for(k=1; j-k >=0  && v2 != 0; k++)
		v2 = v2 && (trees[i][j] > trees[i][j-k]);
	q2=k-1;
	for(k=1; i+k < SIZE && v3 != 0; k++)
		v3 = v3 && (trees[i][j] > trees[i+k][j]);
	q3=k-1;
	for(k=1; i-k >= 0 && v4 != 0; k++)
		v4 = v4 && (trees[i][j] > trees[i-k][j]);
	q4=k-1;
	*ss = q1*q2*q3*q4;
	return (v1  v2 || v3  v4);
}

int main(void){
	FILE *f = fopen("input.txt","r");
	int visible = 4*SIZE-4;
	int trees[SIZE][SIZE];
	char buffer[1024];
	int level = 0;
	while(fgets(buffer,1024,f) != NULL){
		for(int i=0;i<SIZE;i++)
			trees[level][i] = buffer[i] - 48;
		level++;
	}
	int scenic_score = 0;
	for(int i=1;i<SIZE-1;i++){
		for(int j=1;j<SIZE-1;j++){
			int ss;
			visible += verify(trees,i,j,&ss);
			if(ss > scenic_score)
				scenic_score = ss;
		}
	}
	fclose(f);
	printf("%d\n",visible);
	printf("%d\n",scenic_score);
	return 0;
}
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.