Line data Source code
1 : #include "LinewiseInput.h"
2 : #include "Parsing.h"
3 : #include "PuzzleImpl.h"
4 :
5 : #include <algorithm>
6 : #include <array>
7 : #include <cstdint>
8 : #include <numeric>
9 : #include <string_view>
10 :
11 : namespace {
12 :
13 494 : uint64_t parseElveCalories(std::span<std::string_view const> s) {
14 494 : return std::transform_reduce(s.begin(), s.end(), uint64_t(0), std::plus<>(), parseInt<uint64_t>);
15 494 : }
16 : } // namespace
17 :
18 1 : template <> size_t part1<2022, 1>(std::string_view const input) {
19 1 : LinewiseInput const lines(input);
20 :
21 1 : auto linesPerElve = lines.splitOnEmptyLines();
22 :
23 1 : return std::transform_reduce(
24 1 : linesPerElve.begin(), linesPerElve.end(), uint64_t(0),
25 247 : [](auto lhs, auto rhs) { return std::max(lhs, rhs); }, parseElveCalories);
26 1 : }
27 :
28 1 : template <> size_t part2<2022, 1>(std::string_view const input) {
29 1 : LinewiseInput const lines(input);
30 :
31 1 : auto linesPerElve = lines.splitOnEmptyLines();
32 1 : std::vector<uint64_t> calories;
33 1 : calories.reserve(linesPerElve.size());
34 1 : std::ranges::transform(linesPerElve, std::back_inserter(calories), parseElveCalories);
35 :
36 1 : std::partial_sort(calories.begin(), std::next(calories.begin(), 3), calories.end(),
37 273 : [](auto lhs, auto rhs) { return lhs > rhs; });
38 :
39 1 : return std::reduce(calories.begin(), std::next(calories.begin(), 3));
40 1 : }
|