summaryrefslogtreecommitdiffstats
path: root/day03/solution.cpp
blob: d90843f88196026e156c962234c3101aa534e666 (plain) (blame)
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
#include <bits/stdc++.h>

namespace views = std::views;
namespace ranges = std::ranges;

auto parse_input(std::istream& is) {
    const std::string input = {
        std::istreambuf_iterator<char>(is),
        std::istreambuf_iterator<char>()
    };

    auto const lines = views::split(input, '\n')
        | views::filter([](auto&& line) { return not line.empty(); })
        | ranges::to<std::vector<std::string>>();
    return lines;
}

uint64_t compute_joltage(std::string_view bank, int const njolts) {
    std::string s_joltage;
    auto cursor = bank.begin();
    for (int i = njolts-1; i >= 0; --i) {
        size_t const begin = std::distance(bank.begin(), cursor);
        auto const max_elem =
            ranges::max_element(bank
                | views::drop(begin)
                | views::take(bank.size() - begin - i));
        s_joltage += *max_elem;
        cursor = max_elem + 1;
    }
    return std::stoull(s_joltage);
}

void part1(auto input) {
    // Write first part solution here
    uint64_t answer = 0;

    for (auto bank : input) {
        answer += compute_joltage(bank, 2);
    }

    std::println("{}", answer);
}

void part2(auto input) {
    // Write second part solution here
    uint64_t answer = 0;

    for (auto bank : input) {
        auto const joltage = compute_joltage(bank, 12);
        answer += compute_joltage(bank, 12);
    }

    std::println("{}", answer);

}

int main() {
    auto input = parse_input(std::cin);

    part1(input);
    part2(input);

    return 0;
}