summaryrefslogtreecommitdiff
path: root/2023/19/src/part2.cpp
blob: 93bfd659560cedc58ccbed04c9d148e894d86b1f (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
#include <unordered_map>

#include "util.h"

constexpr int FEATS = 4;
constexpr int MIN = 1 - 1;
constexpr int MAX = 4000 + 1;

using Name = std::string;
using Cond = std::tuple<char,char,int>;
using Rule = std::pair<std::vector<Cond>,Name>;
using Bounds = std::array<int,2*FEATS>;
using Workflows = std::unordered_map<Name,Rule>;

const std::unordered_map<char,int> fields{ {'x', 0}, {'m', 2}, {'a', 4}, {'s', 6} };

Cond neg(const Cond& cond)
{
    auto [f,o,v] = cond;
    return { f, (o == '<') ? '>' : '<', v + ((o == '<') ? -1 : 1) };
}

std::pair<std::vector<Rule>,Workflows> parse(const char* file)
{
    using namespace util::separators;

    Workflows workflows;
    std::vector<Rule> arules;

    if (std::ifstream input{ file }; input.is_open())
    {
        std::string line;
        std::getline(input,line);
        while (not line.empty())
        {
            auto bw = line.find('{');
            Name next = line.substr(0, bw);

            std::vector<std::pair<Cond,Name>> conds;
            line = line.substr(bw + 1, line.size() - bw - 2);
            for (auto srule : util::split<COMMA>(std::string_view{ line }))
            {
                auto tokens = util::split<COLON>(srule);
                if (tokens.size() > 1)
                {
                    int val = std::stoi(std::string(tokens[0].substr(2)));
                    conds.push_back({ { tokens[0][0], tokens[0][1], val }, Name{ tokens[1] } });
                }
                else
                {
                    conds.push_back({ { 0, 0, 0 }, Name{ tokens[0] } });
                }
            }

            for (int i = 0; i < conds.size(); ++i)
            {
                auto [cond, name] = conds[i];
                if (name == "R") continue;

                int j{ i - 1 };
                auto [f,o,v] = cond;
                std::vector<Cond> nconds;
                nconds.push_back(f ? cond : neg(std::get<0>(conds[j--])));
                for (; j >= 0; --j)
                {
                    nconds.push_back(neg(std::get<0>(conds[j])));
                }

                if (name == "A")
                {
                    arules.push_back({ std::move(nconds), next });
                }
                else
                {
                    workflows.insert({ name, { std::move(nconds), next } });
                }
            }
            std::getline(input,line);
        }
    }

    return { std::move(arules), std::move(workflows) };
}

Bounds operator&&(const Bounds& a, const Bounds& b)
{
    Bounds c;
    for (int i = 0; i < FEATS; ++i)
    {
        c[2*i] = std::max(a[2*i], b[2*i]);
        c[2*i+1] = std::min(a[2*i+1], b[2*i+1]);
    }
    return c;
}

void operator+=(Bounds& b, const Cond& c)
{
    auto [f,o,v] = c;
    int i{ fields.at(f) };
    if (o == '>')
    {
        b[i] = std::max(b[i],v);
    }
    else
    {
        b[i + 1] = std::min(b[i + 1],v);
    }
}

Bounds compute_bounds(const Workflows& wfs, const Rule& rule)
{
    Bounds bounds{ MIN, MAX, MIN, MAX, MIN, MAX, MIN, MAX };
    auto [conds, next] = rule;
    for (const auto& cond : conds)
    {
        bounds += cond;
    }
    if (next == "in") return { bounds };
    return bounds && compute_bounds(wfs, wfs.at(next));
}

long long combinations(const Bounds& bounds)
{
    long long res{ 1 };
    for (int i = 0; i < FEATS; ++i)
    {
        if (bounds[2*i] >= bounds[2*i+1]) return 0;
        res *= bounds[2*i+1] - bounds[2*i] - 1;
    }
    return res;
}

int main(int argc, char* argv[])
{
    long answer{};

    auto [arules, workflows] = parse(argv[1]);
    for (const auto& rule : arules)
    {
        answer += combinations(compute_bounds(workflows, rule));
    }

    std::cout << answer<< std::endl;
    return 0;
}