aboutsummaryrefslogtreecommitdiffstats
path: root/day8/both.cpp
blob: d60495425ec7eabcdffd3fb0d3739493f3d335e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int visible = 0;
std::vector<std::string> map;

bool checkTree(int x, int y)
{
    char c = map[y][x];
    char v;

    // Left check
    v = '0';
    for (int i = 0; i < x; ++i)
        v = std::max(v, map[y][i]);
    if (c > v)
        return true;

    // Right check
    v = '0';
    for (int i = map[y].size() - 1; i > x; --i)
        v = std::max(v, map[y][i]);
    if (c > v)
        return true;

    // North check
    v = '0';
    for (int i = 0; i < y; ++i)
        v = std::max(v, map[i][x]);
    if (c > v)
        return true;

    // South check
    v = '0';
    for (int i = map.size() - 1; i > y; --i)
        v = std::max(v, map[i][x]);

    return c > v;
}

int scenicScore(int x, int y)
{
    char c = map[y][x];
    int n = 0, s = 0, e = 0, w = 0;

    // Left check
    for (int i = x - 1; i >= 0; --i) {
        ++w;
        if (c <= map[y][i])
            break;
    }

    // Right check
    for (int i = x + 1; i < map[y].size(); ++i) {
        ++e;
        if (c <= map[y][i])
            break;
    }

    // North check
    for (int i = y - 1; i >= 0; --i) {
        ++n;
        if (c <= map[i][x])
            break;
    }

    // South check
    for (int i = y + 1; i < map.size(); ++i) {
        ++s;
        if (c <= map[i][x])
            break;
    }

    std::cout << x << ", " << y << ": " << n << ' ' << s << ' ' << e << ' ' << w << std::endl;
    return n * s * e * w;
}

int main()
{
    // Consume entire input into map
    while (!std::cin.eof()) {
        std::string line;
        std::getline(std::cin, line);
        if (!std::cin.eof())
            map.push_back(line);
    }

    visible = 2 * map[0].size() - 4 + 2 * map.size();
    int sx, sy, sc = 0;

    // For each possibly-hidden tree...
    for (int y = 1; y < map.size() - 1; ++y) {
        for (int x = 1; x < map[y].size() - 1; ++x) {
            if (checkTree(x, y))
                ++visible;
            if (int qq = scenicScore(x, y); qq > sc)
                sx = x, sy = y, sc = qq;
        }
    }

    std::cout << visible << std::endl;
    std::cout << sx << ", " << sy << ": " << sc << std::endl;

    return 0;
}