diff options
Diffstat (limited to 'src/2016/day10/aoc.cpp')
-rw-r--r-- | src/2016/day10/aoc.cpp | 122 |
1 files changed, 121 insertions, 1 deletions
diff --git a/src/2016/day10/aoc.cpp b/src/2016/day10/aoc.cpp index b54ebb2..2246f14 100644 --- a/src/2016/day10/aoc.cpp +++ b/src/2016/day10/aoc.cpp @@ -1,6 +1,126 @@ #include "aoc.h" +#include <map> namespace aoc2016 { +static std::map<int, bot*> bots = {}; +static std::map<int, bot*> outputs = {}; + +static bot* get(int i, std::map<int, bot*>& bs) { + auto p = bs.insert({i, nullptr}); + if (p.second) { + bot* b = new bot; + b->idx = i; + p.first->second = b; + } + return p.first->second; +} + +typedef bot* (*get_method)(int); +bot* get_bot(int i) { return get(i, bots); } +bot* get_output(int i) { return get(i, outputs); } + +static void get_number(const char** pp, int* d) { + const char* p = *pp; + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + *pp = p; +} + +void parse_three(const char* p) { + int is[3] = {0, 0, 0}; + int i{0}; + get_method gets[2] = {get_bot, get_bot}; + while (true) { + if (*p >= '0' && *p <= '9') { + if (i == 1 || i == 2) { + if (*(p - 3) == 'u') { + gets[i - 1] = get_output; + } + // if (*(p - 3) == 'o') { + // gets[i - 1] = get_bot; + // } + } + get_number(&p, is + i); + i++; + } + if (*p == '\n') + break; + p++; + } + // printf("%d %d %d\n", is[0], is[1], is[2]); + bot* b0 = get_bot(is[0]); + bot* b1 = gets[0](is[1]); + b1->is_output = gets[0] == get_output; + bot* b2 = gets[1](is[2]); + b2->is_output = gets[1] == get_output; + b0->outputs[0] = b1; + b1->outputs[1] = b2; +} + +void parse_two(const char* p) { + int is[2] = {0, 0}; + int i{0}; + while (true) { + if (*p >= '0' && *p <= '9') { + get_number(&p, is + i); + i++; + } + if (*p == '\n') + break; + p++; + } + get_bot(is[1])->set(is[0]); +} + +void give(bot* b0, bot* b1, int v, int i) { + if (b1 != nullptr) { + printf("%d give %d value %d\n", b0->idx, b1->idx, v); + if (b1->is_output) { + b1->get(v); + } else { + b1->set(v); + } + b0->vs[i] = 0; + } +} + +int robot_work(int l, int h) { + int idx = INT32_MAX; + while (idx == INT32_MAX) { + for (auto& kv : bots) { + bot* b = kv.second; + if (b->can_give()) { + int l0 = b->vs[0]; + int h0 = b->vs[1]; + if (l0 == l && h0 == h) { + idx = b->idx; + break; + } else { + give(b, b->outputs[0], l0, 0); + give(b, b->outputs[1], h0, 1); + } + } + } + } + return idx; +} + +std::pair<int64_t, int64_t> day10(line_view file) { + per_line(file, [](line_view lv) { + const char* p = lv.line; + if (*p == 'b') { + parse_three(p); + } + if (*p == 'v') { + parse_two(p); + } + return true; + }); + int r = robot_work(17, 61); + printf("%d\n", r); + return {0, 0}; +} -std::pair<int64_t, int64_t> day10(line_view) { return {0, 0}; } } // namespace aoc2016 |