aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-19 22:13:39 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-19 22:13:39 +0800
commita2681bb44aace4c1ae87b84c4e66911d7857dc08 (patch)
treee8383e8e7a18717aaa5ac37b019ea6e3be379ddd
parent191618e44b348edbec0bfe9e03a1f105c3029c39 (diff)
downloadadvent-of-code-a2681bb44aace4c1ae87b84c4e66911d7857dc08.tar.gz
advent-of-code-a2681bb44aace4c1ae87b84c4e66911d7857dc08.zip
2016 day10
-rw-r--r--src/2016/day10/aoc.cpp36
-rw-r--r--src/2016/day10/aoc.h5
-rw-r--r--test/test_2016.cpp2
3 files changed, 24 insertions, 19 deletions
diff --git a/src/2016/day10/aoc.cpp b/src/2016/day10/aoc.cpp
index 5b694d1..79c87b2 100644
--- a/src/2016/day10/aoc.cpp
+++ b/src/2016/day10/aoc.cpp
@@ -77,8 +77,8 @@ void parse_two(const char* p) {
void give(bot* b0, bot* b1, int v, int i) {
if (b1 != nullptr) {
- const char* ds[2] = {"bot", "output"};
- printf("%d give %d[%s] value %d\n", b0->idx, b1->idx, ds[(int)b1->is_output], v);
+ // const char* ds[2] = {"bot", "output"};
+ // printf("%d give %d[%s] value %d\n", b0->idx, b1->idx, ds[(int)b1->is_output], v);
if (b1->is_output) {
b1->get(v);
} else {
@@ -88,25 +88,30 @@ void give(bot* b0, bot* b1, int v, int i) {
}
}
-int robot_work(int l, int h) {
- int idx = INT32_MAX;
- while (idx == INT32_MAX) {
+int64_t robot_work(int l, int h, int* idx) {
+ while (true) {
+ size_t n{0};
for (auto& kv : bots) {
bot* b = kv.second;
if (b->can_give()) {
+ n += 1;
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);
+ *idx = b->idx;
}
+ give(b, b->outputs[0], l0, 0);
+ give(b, b->outputs[1], h0, 1);
}
}
+ if (n == 0)
+ break;
}
- return idx;
+ int64_t v{1};
+ for (int i = 0; i < 3; i++) {
+ v *= get_output(i)->values[0];
+ }
+ return v;
}
void print(int x, bot* b) {
@@ -127,12 +132,9 @@ std::pair<int64_t, int64_t> day10(line_view file) {
}
return true;
});
- int r = robot_work(17, 61);
-
- // for (auto& kv : outputs) {
- // print(kv.first, kv.second);
- // }
- return {r, 0};
+ int idx{INT32_MAX};
+ auto o = robot_work(17, 61, &idx);
+ return {idx, o};
}
} // namespace aoc2016
diff --git a/src/2016/day10/aoc.h b/src/2016/day10/aoc.h
index a7d8d72..ea73529 100644
--- a/src/2016/day10/aoc.h
+++ b/src/2016/day10/aoc.h
@@ -12,9 +12,11 @@ struct bot {
bool is_output = false;
bool can_give() const noexcept { return vs[0] != 0 && vs[1] != 0; }
- void set(int v) {
+ bool set(int v) {
+ bool done{false};
for (int i = 0; i < 2; i++) {
if (vs[i] == 0) {
+ done = true;
vs[i] = v;
break;
}
@@ -22,6 +24,7 @@ struct bot {
if (vs[0] > vs[1]) {
std::swap(vs[0], vs[1]);
}
+ return done;
}
void get(int v) { // bot as output
diff --git a/test/test_2016.cpp b/test/test_2016.cpp
index f21bd14..e42d817 100644
--- a/test/test_2016.cpp
+++ b/test/test_2016.cpp
@@ -103,7 +103,7 @@ TEST_CASE("Balance Bots", "[2016]") {
line_view lv = load_file("../src/2016/day10/input");
auto p = aoc2016::day10(lv);
REQUIRE(147 == p.first);
- REQUIRE(0 == p.second);
+ REQUIRE(55637 == p.second);
}
TEST_CASE("", "[2016]") {