Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Trying to refactor someone else's code from codingame website.
- * I am trying to replace a massive if else block with a switch statement.
- * I cannot figure out why my statement is not equivalent to there's.
- * Sorry for the long post. My Git seems broken lately.
- * Can someone explain please?
- */
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <algorithm>
- #define SWITCH_
- namespace js {
- static constexpr auto L { 8 };
- static constexpr auto C { 8 };
- static auto N { 0 };
- static
- std::vector<std::string> rows = {
- {"########"},
- {"# @ #"},
- {"# X#"},
- {"# XXX #"},
- {"# XX #"},
- {"# XX #"},
- {"# $#"},
- {"########"}
- };
- std::pair<int, int> move(std::string s, std::pair<int, int> lpos) {
- if (s == "SOUTH") { lpos.second++; }
- else if (s == "WEST") { lpos.first--; }
- else if (s == "EAST") { lpos.first++; }
- else if (s == "NORTH") { lpos.second--; }
- return lpos;
- }
- bool isPassable(std::string dir, std::pair<int, int> pos, bool dis) {
- std::pair<int, int> npos = move(dir, pos);
- if (npos.first < 0 || npos.second < 0 || npos.first >= C || npos.second >= L) {
- return false;
- }
- auto c = rows[npos.second][npos.first];
- return !(c == '#' || (c == 'X' && !dis));
- }
- } /* namespace js */
- int main() {
- std::pair<int, int> pos;
- std::vector<std::string> directions = { "SOUTH", "EAST", "NORTH", "WEST" };
- std::vector<std::pair<int, int>> tl;
- std::string out = "";
- auto BM { false };
- js::rows.resize(js::L);
- for (auto i = 0ul; i < js::L; i++) {
- auto f = js::rows[i].find("@");
- if (f != std::string::npos) {
- pos = std::make_pair(f, i);
- }
- for (auto j = 0ul; j < js::rows[i].size(); j++) {
- if (js::rows[i][j] == 'T') {
- tl.emplace_back(j, i);
- }
- }
- }
- std::string dir = "SOUTH";
- /* loop forever */ while (true) {
- if (!js::isPassable(dir, pos, BM)) {
- for (auto const & ds : directions) {
- if (js::isPassable(ds, pos, BM)) {
- dir = ds;
- break;
- }
- }
- }
- out += dir + "\n";
- pos = js::move(dir, pos);
- char block = js::rows[pos.second][pos.first];
- #ifdef SWITCH_
- //My Way...Seems to loop infinitely.
- auto complete { false }; /* flag for loop termination - initially false */
- switch (block) {
- case 'X':
- js::rows[pos.second].replace(pos.first, 1, " ");
- break;
- case '$':
- /* loop should terminate - set flag */
- complete = true;
- break;
- default:
- for (auto const & ds : directions) {
- if (ds[0] == block) {
- dir = ds;
- break;
- }
- }
- }
- if (complete) { /* terminate forever loop */ break; }
- #else
- //Their Way...Works
- if (block == 'X') {
- rows[pos.second].replace(pos.first, 1, " ");
- }
- else if (block == '$') {
- break;
- }
- else {
- for (auto const & ds : directions) {
- if (ds[0] == block) {
- dir = ds;
- /* terminate forever loop */ break;
- }
- }
- }
- #endif
- // Loop check
- js::N++;
- if (js::N > js::C * js::L) {
- out = "LOOP\n";
- break;
- }
- }
- std::cout << out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement