#include "_.h"

int main(int argc, char **argv) {
	
	if (argc != 2)
		return 1;

	char *input = argv[1];
	char **inputs = str_split(strdup(input), '\n');

    int x = 0, y = 0;
	for (int i = 0; inputs[i] != NULL; i++)
        for (int j = 0; inputs[i][j] != '\0'; j++)
            if (inputs[i][j] == '^') {
                y = i;
                x = j;
            }
    
    int direction = 0; // 0 = top; 1 = right; 2 = down; 3 = left
    while (1) {
        if (direction == 0) {
            if (inputs[y - 1] == NULL)
                break;
            if (inputs[y - 1][x] == '#')
                direction++;
            else {
                inputs[y][x] = 'X';
                y--;
            }
        }
        else if (direction == 1) {
            if (x + 1 >= strlen(inputs[y]))
                break;
            if (inputs[y][x + 1] == '#')
                direction++;
            else {
                inputs[y][x] = 'X';
                x++;
            }
        }
        else if (direction == 2) {
            if (inputs[y + 1] == NULL)
                break;
            if (inputs[y + 1][x] == '#')
                direction++;
            else {
                inputs[y][x] = 'X';
                y++;
            }
        }
        else if (direction == 3) {
            if (x - 1 < 0)
                break;
            if (inputs[y][x - 1] == '#')
                direction = 0;
            else {
                inputs[y][x] = 'X';
                x--;
            }
        }
    }

    int count = 1;
	for (int i = 0; inputs[i] != NULL; i++)
        for (int j = 0; inputs[i][j] != '\0'; j++)
            if (inputs[i][j] == 'X')
                count++;

    printf("count: %d\n", count);

	return 0;
}
