Add Puzzle 2021-25-01
This commit is contained in:
83
2021/puzzle-25-01.cc
Normal file
83
2021/puzzle-25-01.cc
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
using Int = unsigned long;
|
||||
|
||||
struct State
|
||||
{
|
||||
void add_line(std::string const& line)
|
||||
{
|
||||
assert(lines_.empty() || lines_[0].size() == line.size());
|
||||
lines_.push_back(line);
|
||||
}
|
||||
|
||||
void print(std::ostream& os) const
|
||||
{
|
||||
for (auto const& line : lines_) {
|
||||
os << line << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
State move_right() const
|
||||
{
|
||||
State result{*this};
|
||||
for (std::size_t row{0}; row < lines_.size(); ++row) {
|
||||
for (std::size_t col{0}; col < lines_[row].size(); ++col) {
|
||||
auto from = lines_[row][col];
|
||||
auto to = lines_[row].at((col + 1) % lines_[row].size());
|
||||
if (from == '>' && to == '.') {
|
||||
result.lines_[row][(col + 1) % lines_[row].size()] = '>';
|
||||
result.lines_[row][col] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
State move_down() const
|
||||
{
|
||||
State result{*this};
|
||||
for (std::size_t row{0}; row < lines_.size(); ++row) {
|
||||
auto const& from_row = lines_[row];
|
||||
auto const& to_row = lines_[(row + 1) % lines_.size()];
|
||||
for (std::size_t col{0}; col < from_row.size(); ++col) {
|
||||
if (from_row[col] == 'v' && to_row[col] == '.') {
|
||||
result.lines_[(row + 1) % lines_.size()][col] = 'v';
|
||||
result.lines_[row][col] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(State const& rhs) const noexcept { return lines_ == rhs.lines_; }
|
||||
|
||||
private:
|
||||
std::vector<std::string> lines_;
|
||||
};
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::string line;
|
||||
State state;
|
||||
|
||||
while (std::getline(std::cin, line) && !line.empty()) {
|
||||
state.add_line(line);
|
||||
}
|
||||
|
||||
std::size_t count{0};
|
||||
while (true) {
|
||||
State s1 = state.move_right().move_down();
|
||||
++count;
|
||||
if (s1 == state) {
|
||||
state.print(std::cout);
|
||||
std::cout << "Done in " << count << " steps\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
state = s1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user