Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

c++

part two took me way to long to figure out, how to consider the difference between a tree being on the edge and seeing 0 and a tree only seeing its neigbor.


#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <functional>


typedef std::vector<std::vector<int>> matrix;
typedef std::function<int(int)> direction_function;


bool inline check_direction(int y, int x, matrix& m, int comp, int& scenic, direction_function get_element) {
    int seen{ 0 };
    for (size_t i = 0; comp > i; ++i) {
        seen++;
        if (get_element(i) >= m[y][x]) {
            scenic *= seen;
            return false;
        }
    }
    scenic *= seen;
    return true;
}


bool isVisible(int y, int x, matrix& m, int& scenic) {
    bool see_top{ check_direction(y, x, m, y, scenic, [&](int i) { return m[y - i - 1][x]; }) };
    bool see_bot{ check_direction(y, x, m, x, scenic, [&](int i) { return m[y][x - i - 1]; }) };
    bool see_left{ check_direction(y, x, m, m.size() - y - 1, scenic, [&](int i) { return m[y + i + 1][x]; }) };
    bool see_right{ check_direction(y, x, m, m[y].size() - 1 - x, scenic, [&](int i) { return m[y][x + i + 1]; }) };

    return see_top || see_left || see_bot || see_right;
}


int main()
{
    std::ifstream file("input.txt");
    std::string buf;
    matrix matrix;

    while (std::getline(file, buf))
    {
        std::vector<int> line;
        for (const char c : buf) {
            line.push_back(c - '0');
        }
        matrix.push_back(line);
    }

    int count{ 0 };
    int max_scenic{ 0 };

    for (std::size_t y{ 0 }; y < matrix.size(); ++y) {
        for (std::size_t x{ 0 }; x < matrix[y].size(); ++x) {
            int scenic{ 1 };
            if (isVisible(y, x, matrix, scenic)) {
                //std::cout << "+";
                ++count;
            }
            else {
                //std::cout << "-";
            }
            max_scenic = std::max(scenic, max_scenic);
        }
        std::cout << '\n';
    }

    std::cout << "Part One: " << count << '\n'
        << "Part Two: " << max_scenic;

    return -1;
}

Jump in the discussion.

No email address required.

Wtf does the parser do for this to happen

???

![](/images/16704955479111755.webp)

Jump in the discussion.

No email address required.

aevann doing the advent of fixing code blocks :marseylaughpoundfist:

Jump in the discussion.

No email address required.

yep

@ihsoy fixed king, edit ur comment and add a linebreak in the middle or something to fix it

Jump in the discussion.

No email address required.

:marseycapylove:

Jump in the discussion.

No email address required.

That's great and all, but I asked for my burger without cheese.

Jump in the discussion.

No email address required.

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