diff --git a/adventofcode/2023/day/21/step_counter.cpp b/adventofcode/2023/day/21/step_counter.cpp index 63291ab..c63437b 100644 --- a/adventofcode/2023/day/21/step_counter.cpp +++ b/adventofcode/2023/day/21/step_counter.cpp @@ -1,7 +1,9 @@ #include +#include #include #include #include +#include #include #include #include @@ -26,23 +28,26 @@ int R, C; void print(grid const& g) { - cout << "\n\n"; + static bool first_call = true; + if (first_call) first_call = false; + else cout << "\033[" << R << "A\033[" << C << "D"; REPEAT(r, R) cout << g[r] << "\n"; - cout << "\n\n"; + using namespace chrono_literals; + this_thread::sleep_for(200ms); } int main(int argc, char* argv[]) { read_input(); - int const num_steps = argc > 1 ? std::stoi(argv[1]) : 64; - std::pair const ans = solve(num_steps); - std::cout << "Part one: " << ans.first << "\nPart two: " << ans.second << '\n'; + int const num_steps = argc > 1 ? stoi(argv[1]) : 64; + pair const ans = solve(num_steps); + cout << "Part one: " << ans.first << "\nPart two: " << ans.second << '\n'; } void read_input() { - std::string line; - while(std::getline(std::cin, line)){ + string line; + while(getline(cin, line)){ G.push_back(line); R++; C = int(line.size()); @@ -54,20 +59,18 @@ vector const deltas{{0,1}, {0,-1}, {1,0} , {-1,0}}; queue paint_grid(grid const& G, grid& g, queue& q) { queue nq; - set seen; + set nqed; while(!q.empty()){ auto p = q.front(); q.pop(); - if(seen.contains(p)) continue; - seen.insert(p); for(auto const& delta : deltas){ int r = p.first + delta.first, c = p.second + delta.second; if(not(0 <= r and r < R and 0 <= c and c < C)) continue; - if(seen.contains({r, c})) continue; + if(nqed.contains({r, c})) continue; if(G[r][c] != '#'){ g[r][c] = 'O'; nq.emplace(r, c); - seen.emplace(r, c); + nqed.emplace(r, c); } } } @@ -77,23 +80,19 @@ queue paint_grid(grid const& G, grid& g, queue& q) queue make_new_positions(grid const& G, queue& q) { queue nq; - unordered_set> seen; + unordered_set> nqed; while(!q.empty()){ - auto const p = q.front(); + ii const p = q.front(); q.pop(); - if(seen.contains(p)) continue; - seen.insert(p); - for(auto const& delta : deltas){ - int r = p.first + delta.first, c = p.second + delta.second; - if(seen.contains({r, c})) continue; + for(ii const& delta : deltas){ + int const r = p.first + delta.first, c = p.second + delta.second; // In part two the map is infinite: //if(not(0 <= r and r < R and 0 <= c and c < C)) continue; - int rr = r, cc = c; - rr = (rr%R+R)%R; //while(rr < 0) rr += R; - cc = (cc%C+C)%C; //while(cc < 0) cc += C; - if(G[rr][cc] != '#'){ + int const rr = ((r % R) + R) % R; //while(rr < 0) rr += R; + int const cc = ((c % C) + C) % C; //while(cc < 0) cc += C; + if(G[rr][cc] != '#' and !nqed.contains({r, c})){ nq.emplace(r, c); - seen.emplace(r, c); + nqed.emplace(r, c); } } } @@ -136,11 +135,11 @@ ii get_start_position(grid const& G) return {}; } -std::pair solve(int num_steps) +pair solve(int num_steps) { ii const start = get_start_position(G); - std::pair ans; + pair ans; { queue q; q.push(start); @@ -148,24 +147,23 @@ std::pair solve(int num_steps) for(int i = 0; i < 64; i++){ g = G; q = paint_grid(G,g,q); + //print(g); } - //print(g); ans.first = static_cast(q.size()); } - /* { queue q; q.push(start); grid g; for(int i = 0; i < num_steps; i++){ q = make_new_positions(G,q); + cout << q.size() << endl; } ans.second = static_cast(q.size()); } - */ - std::cout << "solve_part_two=" << solve_part_two(G, start, num_steps) << std::endl; + //cout << "solve_part_two=" << solve_part_two(G, start, num_steps) << endl; return ans; } diff --git a/nob.cpp b/nob.cpp index bf9cb99..62ba4c3 100644 --- a/nob.cpp +++ b/nob.cpp @@ -171,15 +171,16 @@ int main(int argc, char* argv[]) { GO_REBUILD_URSELF(argc, argv); - build_kattis_c_files(); + //build_kattis_c_files(); //build_custom_cpp_files(); //build_directory_cpp_files("adventofcode"); //build_codeforces_cpp_files(); - work_out_leetcode(); - build_and_run_gtest_file("uva/summing_digits.cpp"); + //work_out_leetcode(); + //build_and_run_gtest_file("uva/summing_digits.cpp"); #if GCC_VERSION > 120000 build_cpp_file("adventofcode/2023/day/01/trebuchet.cpp"); #endif build_cpp_file("adventofcode/2023/day/02/cube_conundrum.cpp"); build_cpp_file("adventofcode/2022/day/25/snafu.cpp"); + build_cpp_file("adventofcode/2023/day/21/step_counter.cpp"); }