查看: 86|回复: 0

C++ 20 Day 3: Rucksack Reorganization

[复制链接]

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2023-3-25 17:51:47 | 显示全部楼层 |阅读模式
#include <iostream>#include <vector>#include <fstream>#include <string>#include <ranges>#include <numeric>#include <algorithm>#include <array>int SumPriority(const std::vector<std::string> &data) {    auto view = [](std::string str) -> int {        auto iter = str.begin() + str.length() / 2;        std::sort(str.begin(), iter);        std::sort(iter, str.end());        std::vector<char> result;        std::set_intersection(str.begin(), iter, iter, str.end(), std::back_inserter(result));        if (std::isupper(result[0])) {            return 27 + result[0] - 'A';        }        return 1 + result[0] - 'a';    };    auto view2 = data                 | std::views::transform(view)                 | std::views::common;    return std::reduce(view2.begin(), view2.end());}int Attachment(std::vector<std::string> data) {    std::ranges::for_each(data, [](auto &str) {        std::ranges::sort(str);    });    int result = 0;    for (auto &&chunk: data | std::views::chunk(3)) {        std::vector<char> tmp, out;        std::ranges::set_intersection(chunk[0].begin(), chunk[0].end(),                                      chunk[1].begin(), chunk[1].end(),                                      std::back_inserter(tmp));        std::ranges::set_intersection(tmp.begin(), tmp.end(),                                      chunk[2].begin(), chunk[2].end(),                                      std::back_inserter(out));        if (std::isupper(out[0])) {            result += 27 + out[0] - 'A';        } else {            result += 1 + out[0] - 'a';        }    }    return result;}int Parse(std::string_view filePath) {    std::vector<std::string> data;    std::ifstream file(filePath.data());    if (!file.is_open()) {        std::cout << "open file failed" << std::endl;        return -1;    }    std::string line;    while (std::getline(file, line)) {        data.push_back(line);    }    std::cout << "SumPriority: " << SumPriority(data) << std::endl;    std::cout << "Attachment: " << Attachment(data) << std::endl;    return 0;}int main() {    Parse(R"(D:\AllCodeProjects\CLionProject\AdventofCode2022\code\03.txt)");}
--- Day 3: Rucksack Reorganization ---

---第三天:背包重组---

One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged.
一个精灵有装载所有的重要工作 帆布背包 提供了 丛林旅程。不幸的是,那个精灵没有完全按照包装说明,所以现在有几件物品需要重新排列。
Each rucksack has two large compartments. All items of a given type are meant to go into exactly one of the two compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.
每个帆布背包有两个大。给定类型的所有项目都应该恰好放入这两个隔室中的一个。打包的精灵没有遵守这个规则,每个背包只有一种物品类型。
The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, a and A refer to different types of items).
精灵们已经列出了每个背包里的所有物品(你的谜题输入),但是他们需要你的帮助来找到错误。每个项目类型都由一个小写或大写字母标识(即a和A表示不同类型的项目)。
The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment.
每个背包的物品列表都是以字符的形式给出的,都在一行上。一个给定的背包在它的两个隔间中总是有相同数量的物品,所以字符的前半部分代表第一个隔间中的物品,而字符的后半部分代表第二个隔间中的物品。
For example, suppose you have the following list of contents from six rucksacks:
例如,假设您有以下六个背包的内容列表:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

  • The first rucksack contains the items vJrwpWtwJgWrhcsFMMfFFhFp, which means its first compartment contains the items vJrwpWtwJgWr, while the second compartment contains the items hcsFMMfFFhFp. The only item type that appears in both compartments is lowercase p.
  • 第一背包包含项目 vJrwpWtwJgWrhcsFMMfFFhFp,这意味着其第一隔室包含项目vJrwpWtwJgWr,而第二隔室包含项目hcsFMMfFFhFp。出现在这两个隔离舱中的唯一项类型是小写的p
  • The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only item type that appears in both compartments is uppercase L.
  • 第二个背包的隔间包含jqHRNqRjqzjGDLGL和rsFMfFZsrLrFZsSL。出现在两个分隔栏中的唯一项类型是大写L
  • The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item type is uppercase P.
  • 第三个背包的隔间包含 PmmdzqPrV和vPwwTWBwg;唯一常见的物品类型是大写的P
  • The fourth rucksack's compartments only share item type v.
  • 第四个背包的隔间只共享物品类型v
  • The fifth rucksack's compartments only share item type t.
  • 第五个背包的隔间只共享物品类型t
  • The sixth rucksack's compartments only share item type s.
  • 第六个背包的隔层只共享物品类型 S的
To help prioritize item rearrangement, every item type can be converted to a priority:
为了帮助确定项目重排的优先级,可以将每个项目类型转换为优先级

  • Lowercase item types a through z have priorities 1 through 26.
  • 小写项目类型 a到z的优先级为1到26。
  • Uppercase item types A through Z have priorities 27 through 52.
  • 大写项目类型 A到Z的优先级为27到52。
In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (p), 38 (L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is 157.
在上面的例子中,出现在每个背包的两个隔间中的物品类型的优先级是16(p)、38(L)、42(P)、22(v)、20(t)和19(s);这些优先级的总和是157
Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types?
找出出现在每个背包的两个隔间中的物品类型。这些物料类型的优先级之和是多少?
Your puzzle answer was 7581.
你的谜题答案是7581。
--- Part Two ---

---第二部分---

As you finish identifying the misplaced items, the Elves come to you with another issue.
当你完成识别放错地方的物品时,精灵们带着另一个问题来找你。
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a group's badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves will be carrying any other item type.
为了安全起见,精灵们分成三组。每个精灵都有一个徽章来识别他们的团体。为了提高效率,在每组三个精灵中,徽章是 所有三个Elf都携带的唯一项目类型。也就是说,如果组的工卡为项目类型 B ,则所有三个Elf都将具有项目类型 B在他们背包的某个地方,最多两个精灵会携带任何其他类型的物品。
The problem is that someone forgot to put this year's updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached.
问题是,有人忘了把今年更新的真实性标签上的徽章。所有的徽章都需要从背包里拿出来,这样新的真实性贴纸才能贴上。
Additionally, nobody wrote down which item type corresponds to each group's badges. The only way to tell which item type is the right one is by finding the one item type that is common between all three Elves in each group.
此外,没有人写下每个小组的徽章对应的项目类型。唯一的方法来告诉哪一个项目类型是正确的是通过找到一个项目类型是共同的所有三个精灵在每一组。
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group's rucksacks are the first three lines:
列表中每组三行对应一个组,但每个组可以有不同的工卡项目类型。因此,在上例中,第一组的背包是前三行:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwgAnd the second group's rucksacks are the next three lines:
第二组的背包是下面三行:
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDwIn the first group, the only item type that appears in all three rucksacks is lowercase r; this must be their badges. In the second group, their badge item type must be Z.
在第一组中,所有三个背包中出现的唯一物品类型是小写的r;这一定是他们的徽章。在第二组中,其工卡项目类型必须为Z。
Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (r) for the first group and 52 (Z) for the second group. The sum of these is 70.
仍必须确定这些项目的优先级,以便组织粘贴标签的工作: 在这里,他们是18岁( r )和52(Z型)为第二组。这些的总和是 70
Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types?
找到与每个三精灵组的徽章相对应的物品类型。这些物料类型的优先级之和是多少?
Your puzzle answer was 2525.
你的谜题答案是2525.
Both parts of this puzzle are complete! They provide two gold stars: **
这个谜题的两部分都完成了!他们提供两颗金星:**
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表