diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-19 16:13:01 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-19 16:13:01 +0800 |
commit | 6b38eb27ba31546c49dee6934700172380a14325 (patch) | |
tree | 65ef700599f85b236bc5fd3d3422242997379e81 | |
parent | 1ec354bbbde7d9f058d09d09be6a32ae3c4a814e (diff) | |
download | advent-of-code-6b38eb27ba31546c49dee6934700172380a14325.tar.gz advent-of-code-6b38eb27ba31546c49dee6934700172380a14325.zip |
2016 day10
-rw-r--r-- | src/2016/day10/aoc.cpp | 122 | ||||
-rw-r--r-- | src/2016/day10/aoc.h | 27 | ||||
-rw-r--r-- | src/2016/day10/input | 1 | ||||
-rw-r--r-- | src/2016/day10/input0 | 1 |
4 files changed, 149 insertions, 2 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 diff --git a/src/2016/day10/aoc.h b/src/2016/day10/aoc.h index fb3f80b..a7d8d72 100644 --- a/src/2016/day10/aoc.h +++ b/src/2016/day10/aoc.h @@ -3,5 +3,30 @@ #include <vector> namespace aoc2016 { + +struct bot { + int idx; + int vs[2] = {0, 0}; + std::vector<int> values; + bot* outputs[2] = {nullptr, nullptr}; + bool is_output = false; + + bool can_give() const noexcept { return vs[0] != 0 && vs[1] != 0; } + void set(int v) { + for (int i = 0; i < 2; i++) { + if (vs[i] == 0) { + vs[i] = v; + break; + } + } + if (vs[0] > vs[1]) { + std::swap(vs[0], vs[1]); + } + } + + void get(int v) { // bot as output + values.push_back(v); + } +}; std::pair<int64_t, int64_t> day10(line_view); -} +} // namespace aoc2016 diff --git a/src/2016/day10/input b/src/2016/day10/input index 0d8937f..cf48b46 100644 --- a/src/2016/day10/input +++ b/src/2016/day10/input @@ -229,3 +229,4 @@ bot 121 gives low to bot 51 and high to bot 81 bot 122 gives low to bot 200 and high to bot 7 bot 96 gives low to bot 25 and high to bot 63 bot 94 gives low to bot 23 and high to bot 19 + diff --git a/src/2016/day10/input0 b/src/2016/day10/input0 index e9ad2b4..219b5e7 100644 --- a/src/2016/day10/input0 +++ b/src/2016/day10/input0 @@ -4,3 +4,4 @@ value 3 goes to bot 1 bot 1 gives low to output 1 and high to bot 0 bot 0 gives low to output 2 and high to output 0 value 2 goes to bot 2 + |