Skip to content

Commit

Permalink
Improve bookeeping in searches. Update print making nice visualization.
Browse files Browse the repository at this point in the history
Visualization using escape code.
Refactor.
For part two, printing number garden plots at each step. Checking the
sequence and difference sequence in Python shows kind of exponential
and a periodic-like function with an exponential trend, respectively.
  • Loading branch information
iglesias committed May 27, 2024
1 parent 9c430a6 commit 5999f7e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
58 changes: 28 additions & 30 deletions adventofcode/2023/day/21/step_counter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <cassert>
#include <chrono>
#include <iostream>
#include <queue>
#include <set>
#include <thread>
#include <string>
#include <unordered_set>
#include <utility>
Expand All @@ -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());
Expand All @@ -54,20 +59,18 @@ vector<ii> const deltas{{0,1}, {0,-1}, {1,0} , {-1,0}};
queue<ii> paint_grid(grid const& G, grid& g, queue<ii>& q)
{
queue<ii> nq;
set<ii> seen;
set<ii> 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);
}
}
}
Expand All @@ -77,23 +80,19 @@ queue<ii> paint_grid(grid const& G, grid& g, queue<ii>& q)
queue<ii> make_new_positions(grid const& G, queue<ii>& q)
{
queue<ii> nq;
unordered_set<ii, boost::hash<ii>> seen;
unordered_set<ii, boost::hash<ii>> 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);
}
}
}
Expand Down Expand Up @@ -136,36 +135,35 @@ ii get_start_position(grid const& G)
return {};
}

std::pair<int, unsigned long long> solve(int num_steps)
pair<int, unsigned long long> solve(int num_steps)
{
ii const start = get_start_position(G);

std::pair<int, unsigned long long> ans;
pair<int, unsigned long long> ans;
{
queue<ii> q;
q.push(start);
grid g;
for(int i = 0; i < 64; i++){
g = G;
q = paint_grid(G,g,q);
//print(g);
}
//print(g);
ans.first = static_cast<int>(q.size());
}

/*
{
queue<ii> 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<int>(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;
}
7 changes: 4 additions & 3 deletions nob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

1 comment on commit 5999f7e

@iglesias
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sample input:

Figure_1

Puzzle input:

Figure_2

Please sign in to comment.