From ead6a8d61d3d03d44e457187cd2725c37922a2d9 Mon Sep 17 00:00:00 2001 From: kaiwu Date: Sat, 11 Feb 2023 17:53:32 +0800 Subject: 2017 day18 --- src/2017/day18/aoc.cpp | 90 ++++++++++++++++++++++++++---------------------- src/2017/day19/README.md | 26 ++++++++++++++ test/test_2017.cpp | 2 +- 3 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/2017/day18/aoc.cpp b/src/2017/day18/aoc.cpp index 9cef4c9..7086681 100644 --- a/src/2017/day18/aoc.cpp +++ b/src/2017/day18/aoc.cpp @@ -28,14 +28,21 @@ static int64_t get_number(const char* p, int64_t rs[26]) { return d; } -typedef void (*f18)(size_t*, const char*, const char*, int64_t rs[26], std::deque* qs[2]); +struct message_queue { + std::deque* qs; + std::deque* qr; + size_t c; +}; -static void snd(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { - qs[0]->push_back(get(*p1, rs)); +typedef void (*f18)(size_t*, const char*, const char*, int64_t rs[26], message_queue& q); + +static void snd(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { + q.qs->push_back(get(*p1, rs)); + q.c += 1; *i += 1; } -static void rcv0(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { +static void rcv0(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { auto d = get_number(p1, rs); if (d != 0) { // printf("%ld\n", sounds.back()); @@ -44,36 +51,36 @@ static void rcv0(size_t* i, const char* p1, const char* p2, int64_t rs[26], std: *i += 1; } -static void rcv1(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { - if (!qs[1]->empty()) { - auto x = qs[1]->front(); - qs[1]->pop_front(); - get(*p1, rs) = x; - *i += 1; - } -} +// static void rcv1(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { +// if (!q.qr->empty()) { +// auto x = q.qr->front(); +// q.qr->pop_front(); +// get(*p1, rs) = x; +// *i += 1; +// } +// } -static void set(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { +static void set(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& qs) { get(*p1, rs) = get_number(p2, rs); *i += 1; } -static void add(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { +static void add(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { get(*p1, rs) += get_number(p2, rs); *i += 1; } -static void mul(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { +static void mul(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { get(*p1, rs) *= get_number(p2, rs); *i += 1; } -static void mod(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { +static void mod(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { get(*p1, rs) %= get_number(p2, rs); *i += 1; } -static void jgz(size_t* i, const char* p1, const char* p2, int64_t rs[26], std::deque* qs[2]) { +static void jgz(size_t* i, const char* p1, const char* p2, int64_t rs[26], message_queue& q) { auto d0 = get_number(p1, rs); auto d1 = get_number(p2, rs); *i += d0 > 0 ? d1 : 1; @@ -87,8 +94,7 @@ static void jgz(size_t* i, const char* p1, const char* p2, int64_t rs[26], std:: // printf("\n"); // } -static size_t exec(size_t index, const std::vector& todos, int64_t rs[26], std::deque* qs[2], - f18 rcv) { +static size_t exec(size_t index, const std::vector& todos, int64_t rs[26], message_queue& q, f18 rcv) { if (index < todos.size()) { f18 fs[] = {snd, set, add, mul, mod, jgz, rcv}; const char* cs[] = {"snd", "set", "add", "mul", "mod", "jgz", "rcv"}; @@ -105,7 +111,7 @@ static size_t exec(size_t index, const std::vector& todos, int64_t rs for (size_t i = 0; i < 7; i++) { if (match(p, cs[i])) { - fs[i](&index, p + 4, p + 6, rs, qs); + fs[i](&index, p + 4, p + 6, rs, q); break; } } @@ -117,33 +123,34 @@ static int64_t part1(const std::vector& todos) { size_t index{0}; int64_t rs[26] = {0}; std::deque q; - std::deque* qs[2] = {&q, &q}; + message_queue mq = {&q, &q, 0}; while (index < todos.size()) { - index = exec(index, todos, rs, qs, rcv0); + index = exec(index, todos, rs, mq, rcv0); } return q.back(); } -static void part2(const std::vector& todos) { - size_t i0{0}; - size_t i1{0}; - - int64_t rs0[26] = {0}; - get('p', rs0) = 0; - int64_t rs1[26] = {0}; - get('p', rs1) = 1; - - std::deque q0; - std::deque q1; - std::deque* qs0[2] = {&q1, &q0}; - std::deque* qs1[2] = {&q0, &q1}; - - while (i0 < todos.size() && i1 < todos.size()) { - i0 = exec(i0, todos, rs0, qs0, rcv1); - i1 = exec(i1, todos, rs1, qs1, rcv1); - } -} +// static void part2(const std::vector& todos) { +// size_t i0{0}; +// size_t i1{0}; +// +// int64_t rs0[26] = {0}; +// get('p', rs0) = 0; +// int64_t rs1[26] = {0}; +// get('p', rs1) = 1; +// +// std::deque q0; +// std::deque q1; +// message_queue mq0 = {&q1, &q0, 0}; +// message_queue mq1 = {&q0, &q1, 0}; +// +// while (i0 < todos.size() && i1 < todos.size()) { +// i0 = exec(i0, todos, rs0, mq0, rcv1); +// i1 = exec(i1, todos, rs1, mq1, rcv1); +// printf("%zu %zu\n", mq0.c, mq1.c); +// } +// } std::pair day18(line_view file) { std::vector todos; @@ -153,6 +160,7 @@ std::pair day18(line_view file) { }); auto t0 = part1(todos); + // part2(todos); return {t0, 0}; } } // namespace aoc2017 diff --git a/src/2017/day19/README.md b/src/2017/day19/README.md index e69de29..50ca463 100644 --- a/src/2017/day19/README.md +++ b/src/2017/day19/README.md @@ -0,0 +1,26 @@ +--- Day 19: A Series of Tubes --- + +Somehow, a network packet got lost and ended up here. It's trying to follow a routing diagram (your puzzle input), but it's confused about where to go. + +Its starting point is just off the top of the diagram. Lines (drawn with |, -, and +) show the path it needs to take, starting by going down onto the only line connected to the top of the diagram. It needs to follow this path until it reaches the end (located somewhere within the diagram) and stop there. + +Sometimes, the lines cross over each other; in these cases, it needs to continue going the same direction, and only turn left or right when there's no other option. In addition, someone has left letters on the line; these also don't change its direction, but it can use them to keep track of where it's been. For example: + + | + | +--+ + A | C + F---|----E|--+ + | | | D + +B-+ +--+ + +Given this diagram, the packet needs to take the following path: + + Starting at the only line touching the top of the diagram, it must go down, pass through A, and continue onward to the first +. + Travel right, up, and right, passing through B in the process. + Continue down (collecting C), right, and up (collecting D). + Finally, go all the way left through E and stopping at F. + +Following the path to the end, the letters it sees on its path are ABCDEF. + +The little packet looks up at you, hoping you can help it find the way. What letters will it see (in the order it would see them) if it follows the path? (The routing diagram is very wide; make sure you view it without line wrapping.) + diff --git a/test/test_2017.cpp b/test/test_2017.cpp index 5b3a58e..9cc9421 100644 --- a/test/test_2017.cpp +++ b/test/test_2017.cpp @@ -186,7 +186,7 @@ TEST_CASE("Duet", "[2017]") { } -TEST_CASE("", "[2017]") { +TEST_CASE("A Series of Tubes", "[2017]") { line_view lv = load_file("../src/2017/day19/input"); auto p = aoc2017::day19(lv); REQUIRE(0 == p.first); -- cgit v1.2.3