C++20 Advent of Code 可见树 Day 8: Treetop Tree House

C++20 Advent of Code 可见树 Day 8: Treetop Tree House

Day 8 - Advent of Code 2022

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <ranges>
#include <numeric>
#include <algorithm>
#include <array>
#include <map>
#include <unordered_map>
#include <optional>

size_t CountVisible(std::vector<std::string> const &map) {
    size_t cnt = 0;
    char max = '0' - 1;
    auto mapCopy = map;

    auto count = [&](size_t ridx, size_t cidx) {
        if (map[ridx][cidx] <= max) return;

        if (mapCopy[ridx][cidx] != 'X')
            cnt++;

        max = map[ridx][cidx];
        mapCopy[ridx][cidx] = 'X';
    };

    for (auto ridx: std::views::iota(0u, map.size())) {
        max = '0' - 1;

        for (auto cidx: std::views::iota(0u, map[ridx].length())) {
            count(ridx, cidx);
        }

        max = '0' - 1;

        for (auto cidx: std::views::iota(0u, map[ridx].length()) | std::views::reverse) {
            count(ridx, cidx);
        }
    }
    for (auto cidx: std::views::iota(0u, map[0].length())) {

        max = '0' - 1;
        for (auto ridx: std::views::iota(0u, map.size()))
            count(ridx, cidx);

        max = '0' - 1;
        for (auto ridx: std::views::iota(0u, map.size()) | std::views::reverse)
            count(ridx, cidx);
    }

    return cnt;


}


size_t bestScore(const std::vector<std::string> &map) {
    std::vector<std::vector<size_t>> scores(map.size(), std::vector<size_t>(map[0].length(), 1));

    for (auto ridx: std::views::iota(1u, map.size() - 1)) {

        std::vector<size_t> block(10, 0);

        for (auto cidx: std::views::iota(1u, map[ridx].length() - 1)) {
            char c = map[ridx][cidx];

            scores[ridx][cidx] *= cidx - block[c - '0'];

            for (auto val: std::views::iota(0, c - '0' + 1))
                block[val] = cidx;
        }

        block = std::vector<size_t>(10, map[ridx].length() - 1); // reset
        for (auto cidx: std::views::iota(1u, map[ridx].length() - 1) | std::views::reverse) {
            char c = map[ridx][cidx];

            scores[ridx][cidx] *= block[c - '0'] - cidx;
            for (auto val: std::views::iota(0, c - '0' + 1))
                block[val] = cidx;
        }
    }
    size_t max = 0;
    for (auto cidx: std::views::iota(1u, map[0].length() - 1)) {
        std::vector<size_t> block(10, 0);
        for (auto ridx: std::views::iota(1u, map.size() - 1)) {
            char c = map[ridx][cidx];

            scores[ridx][cidx] *= ridx - block[c - '0'];
            for (auto val: std::views::iota(0, c - '0' + 1))
                block[val] = ridx;
        }

        block = std::vector<size_t>(10, map.size() - 1);
        for (auto ridx: std::views::iota(1u, map.size() - 1) | std::views::reverse) {
            char c = map[ridx][cidx];

            scores[ridx][cidx] *= block[c - '0'] - ridx;

            if (scores[ridx][cidx] > max)
                max = scores[ridx][cidx];
            for (auto val: std::views::iota(0, c - '0' + 1))
                block[val] = ridx;
        }
    }
    return max;
}


int Parse(std::string_view path) {
    std::ifstream ifs(path.data());
    if (!ifs.is_open()) {
        std::cout << "open file failed" << std::endl;
        return -1;
    }
    std::vector<std::string> data;

    std::ranges::copy(std::views::istream<std::string>(ifs), std::back_inserter(data));

    std::cout << "CountVisible: " << CountVisible(data) << std::endl;
    std::cout << "bestScore: " << bestScore(data) << std::endl;
}

int main() {
    Parse(R"(D:\AllCodeProjects\CLionProject\AdventofCode2022\code\08.txt)");
}

参考:HappyCerberus/moderncpp-aoc-2022: Solutions for Advent of Code 2022 using Modern C++. (github.com)

— Day 8: Treetop

Tree House — —第八天:树顶树屋—

The expedition comes across a peculiar patch of tall trees all planted carefully in a grid. The Elves explain that a previous expedition planted these trees as a reforestation effort. Now, they’re curious if this would be a good location for a tree house.
探险队遇到了一片特殊的高大树木,它们都精心地种植在一个网格中。精灵解释说,以前的探险队种植这些树是为了重新造林。现在,他们很好奇这里是不是个好地方 树屋.

First, determine whether there is enough tree cover here to keep a tree house hidden. To do this, you need to count the number of trees that are visible from outside the grid when looking directly along a row or column.
首先,确定是否有足够的树木覆盖在这里保持一个树屋 隐藏.要做到这一点,您需要计算 从栅格外部可见当直接沿着行或列看时。

The Elves have already launched a quadcopter to generate a map with the height of each tree (your puzzle input). For example:
精灵已经发动了一场 四轴飞行器 以生成具有每棵树的高度的地图( 你的谜题输入).例如:

30373
25512
65332
33549
35390

Each tree is represented as a single digit whose value is its height, where 0 is the shortest and 9 is the tallest.
每棵树都表示为一个数字,其值为树高,其中0表示最短,9表示最高。

A tree is visible if all of the other trees between it and an edge of the grid are shorter than it. Only consider trees in the same row or column; that is, only look up, down, left, or right from any given tree.
一棵树 可见的 如果它和网格的边之间的所有其他树 较短只考虑同一行或同一列的树;也就是说,仅从任何给定树向上、向下、向左或向右查找。

All of the trees around the edge of the grid are visible - since they are already on the edge, there are no trees to block the view. In this example, that only leaves the interior nine trees to consider:
栅格边缘周围的所有树都是可见的-因为它们已经在边缘上,所以没有树阻挡视图。在这个例子中,只剩下内部的九棵树需要考虑:

  • The top-left 5 is visible from the left and top. (It isn’t visible from the right or bottom since other trees of height 5 are in the way.)
    左上角 5可见的从左到上。(It从右侧或底部看不到,因为其他树的高度 5 挡道。)
  • The top-middle 5 is visible from the top and right.
    中上层 5可见的从右上方开始。
  • The top-right 1 is not visible from any direction; for it to be visible, there would need to only be trees of height 0 between it and an edge.
    右上角 1 从任何方向均不可见; 要使它可见,只需要树的高度0在它和边缘之间。
  • The left-middle 5 is visible, but only from the right.
    左中部 5可见的,但仅从右侧可见。
  • The center 3 is not visible from any direction; for it to be visible, there would need to be only trees of at most height 2 between it and an edge.
    该中心 3 从任何方向均不可见; 要使其可见,最多只需要高度为的树2在它和边缘之间。
  • The right-middle 3 is visible from the right.
    右中 3可见的从右边。
  • In the bottom row, the middle 5 is visible, but the 3 and 4 are not.
    在最下面一排中间 5可见的 ,但是 3 以及 4不是。

With 16 trees visible on the edge and another 5 visible in the interior, a total of *21* trees are visible in this arrangement.
16棵树在边缘可见,另外5棵树在内部可见,总共*21*棵树在这种排列中可见。

Consider your map; how many trees are visible from outside the grid?
考虑你的地图;从网格外可以看到多少棵树?

Your puzzle answer was 1681.
你的谜题答案是1681.

— Part Two — —第二部分—

Content with the amount of tree cover available, the Elves just need to know the best spot to build their tree house: they would like to be able to see a lot of trees.
满足于现有的树木覆盖量,精灵们只需要知道建造树屋的最佳地点:他们希望能看到很多

To measure the viewing distance from a given tree, look up, down, left, and right from that tree; stop if you reach an edge or at the first tree that is the same height or taller than the tree under consideration. (If a tree is right on the edge, at least one of its viewing distances will be zero.)
要测量与给定树的观看距离,请从该树向上、向下、向左和向右看;如果你到达一个边缘或在第一棵树是相同的高度或高于正在考虑的树停止。(If如果树正好在边缘上,则它的观看距离中的至少一个将为零。

The Elves don’t care about distant trees taller than those found by the rules above; the proposed tree house has large eaves to keep it dry, so they wouldn’t be able to see higher than the tree house anyway.
精灵不在乎远处的树比上面的规则所发现的树高;计划中的树屋有大屋檐,可以保持干燥,这样他们就看不到比树屋更高的地方了。

In the example above, consider the middle 5 in the second row:
在上面的例子中,考虑中间 5在第二行中:

30373
25512
65332
33549
35390
  • Looking up, its view is not blocked; it can see *1* tree (of height 3).
    抬头看,它的视线不被遮挡;它可以看到*1*棵树(高度为3)。
  • Looking left, its view is blocked immediately; it can see only *1* tree (of height 5, right next to it).
    向左看,它的视野立刻被挡住;它只能看到*1*棵树(树高5,就在它旁边)。
  • Looking right, its view is not blocked; it can see *2* trees.
    向右看,它的视线不受阻挡;可以看到*两*棵树。
  • Looking down, its view is blocked eventually; it can see *2* trees (one of height 3, then the tree of height 5 that blocks its view).
    向下看,它的视线终被遮挡; 它能看见 *2* 树(高度之一 3 ,那么树的高度 5 阻挡其视野)。

A tree’s scenic score is found by multiplying together its viewing distance in each of the four directions. For this tree, this is *4* (found by multiplying 1 * 1 * 2 * 2).
一棵树的风景得分是通过它在四个方向上的观看距离相乘来得到的。对于这棵树,这是*4*(乘以1 * 1 * 2 * 2)。

However, you can do even better: consider the tree of height 5 in the middle of the fourth row:
你可以做得更好: 考虑树的高度5在第四行的中间:

30373
25512
65332
33549
35390
  • Looking up, its view is blocked at *2* trees (by another tree with a height of 5).
    抬头看,它的视线被挡住在 *2* 树(由另一棵树高度为 5 ).
  • Looking left, its view is not blocked; it can see *2* trees.
    向左看,它的视野没有被遮挡;可以看到*两*棵树。
  • Looking down, its view is also not blocked; it can see *1* tree.
    向下看,它的视野也不被遮挡;可以看到*一*棵树。
  • Looking right, its view is blocked at *2* trees (by a massive tree of height 9).
    向右看,它的视野被挡住在 *2* 树(由一棵高大的大树 9 ).

This tree’s scenic score is *8* (2 * 2 * 1 * 2); this is the ideal spot for the tree house.
这棵树的风景评分是*8*2 * 2 * 1 * 2);这是树屋的理想地点。

Consider each tree on your map. What is the highest scenic score possible for any tree?
查看地图上的每棵树。任何一棵树的最高风景评分是多少?

Your puzzle answer was 201684.
你的谜题答案是201684

Both parts of this puzzle are complete! They provide two gold stars: **
这个谜题的两部分都完成了!他们提供两颗金星:**

At this point, you should return to your Advent calendar and try another puzzle.
在这一点上,你应该回到你的降临节日历,并尝试另一个难题。

If you still want to see it, you can get your puzzle input.
如果你还想看的话,你可以输入你的谜题

You can also [Share] this puzzle.
您也可以[分享 ] 这个难题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值