-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.hpp
130 lines (119 loc) · 4.16 KB
/
day04.hpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "range/v3/view/iota.hpp"
#include <charconv>
#include <execution>
#include <string_view>
#include <fmt/core.h>
#include <fmt/ranges.h>
#include <range/v3/all.hpp>
#include <exec/inline_scheduler.hpp>
#include <stdexec/execution.hpp>
namespace {
using std::string_view_literals::operator""sv;
namespace rv = ranges::views;
constexpr std::string_view DIGITS{"0123456789"sv};
auto read_numbers(std::string_view nums) {
std::vector<int> values;
int v;
while (not nums.empty()) {
auto start = nums.find_first_of(DIGITS);
if (start == std::string_view::npos) {
break;
}
auto stop = nums.find_first_not_of(DIGITS, start);
if (stop == std::string_view::npos) {
stop = nums.size();
}
auto const n = nums.substr(start, stop - start);
std::from_chars(n.begin(), n.end(), v);
values.push_back(v);
nums.remove_prefix(stop);
}
std::sort(values.begin(), values.end());
return values;
}
struct card {
std::vector<int> winners;
std::vector<int> all;
card(std::string_view line) {
line.remove_prefix(line.find_first_of(':'));
size_t const sep = line.find_first_of('|');
winners = read_numbers(line.substr(0, sep));
all = read_numbers(line.substr(sep + 1));
}
};
auto parse([[maybe_unused]] auto scheduler, std::string_view input) {
std::vector<card> cards;
while (not input.empty()) {
auto split = input.find_first_of('\n');
if (split == std::string_view::npos) {
break;
}
auto line = input.substr(0, split);
if (line.empty()) {
break;
}
cards.emplace_back(line);
input.remove_prefix(split + 1);
}
return cards;
}
auto part1(auto scheduler, std::vector<card> const &cards) {
std::span games{cards};
auto process = [=](unsigned i, std::span<int> totals) {
auto &[winners, all] = games[i];
auto set = rv::set_intersection(all, winners);
auto const count{ranges::distance(set.begin(), set.end())};
if (count > 0) {
totals[i] = (1 << (count - 1));
}
};
auto [value] =
stdexec::sync_wait(
stdexec::just(std::vector<int>(games.size(), 0)) |
stdexec::let_value([=](std::vector<int> &totals) {
return stdexec::transfer_just(scheduler, std::span{totals}) |
stdexec::bulk(games.size(), process) |
stdexec::transfer(exec::inline_scheduler{}) |
stdexec::then([=](std::span<int> totals) {
return std::reduce(std::execution::par_unseq,
totals.begin(), totals.end());
});
}))
.value();
return value;
}
auto part2(auto scheduler, std::vector<card> const &cards,
[[maybe_unused]] int part1_answer) {
std::span games{cards};
auto process = [=](unsigned i, std::span<int> totals, std::span<int> counts) {
auto &[winners, all] = games[i];
auto set = rv::set_intersection(all, winners);
totals[i] = ranges::distance(set.begin(), set.end());
counts[i] = 1;
};
auto [value] =
stdexec::sync_wait(
stdexec::just(std::vector<int>(games.size(), 0),
std::vector<int>(games.size(), 0)) |
stdexec::let_value(
[=](std::vector<int> &totals, std::vector<int> &counts) {
return stdexec::transfer_just(scheduler, std::span{totals},
std::span{counts}) |
stdexec::bulk(games.size(), process) |
stdexec::transfer(exec::inline_scheduler{}) |
stdexec::then([=](std::span<int> totals,
std::span<int> counts) {
for (auto [i, v] : rv::enumerate(totals)) {
for (auto &ticket :
counts | rv::drop(i + 1) | rv::take(v)) {
ticket += counts[i];
}
}
return std::reduce(std::execution::par_unseq,
counts.begin(), counts.end(), 0);
});
}))
.value();
return value;
}
} // namespace