Add .clang-format and apply it
This commit is contained in:
@@ -16,8 +16,10 @@ enum Edge { Top, Left, Bottom, Right };
|
||||
|
||||
struct PictureArray;
|
||||
|
||||
struct Picture {
|
||||
Picture(std::string id, std::istream &is) : in_use_(false) {
|
||||
struct Picture
|
||||
{
|
||||
Picture(std::string id, std::istream& is) : in_use_(false)
|
||||
{
|
||||
assert(id.substr(0, 5) == "Tile ");
|
||||
id_ = std::stoul(id.substr(5));
|
||||
std::string line;
|
||||
@@ -29,22 +31,24 @@ struct Picture {
|
||||
}
|
||||
}
|
||||
|
||||
Picture(PictureArray const &array);
|
||||
Picture(PictureArray const& array);
|
||||
|
||||
Picture(Picture const &) = delete;
|
||||
Picture &operator=(Picture const &) = delete;
|
||||
Picture(Picture &&) = default;
|
||||
Picture &operator=(Picture &&) = default;
|
||||
Picture(Picture const&) = delete;
|
||||
Picture& operator=(Picture const&) = delete;
|
||||
Picture(Picture&&) = default;
|
||||
Picture& operator=(Picture&&) = default;
|
||||
|
||||
void flip() {
|
||||
for (auto &r : rows_) {
|
||||
void flip()
|
||||
{
|
||||
for (auto& r : rows_) {
|
||||
std::reverse(r.begin(), r.end());
|
||||
}
|
||||
}
|
||||
|
||||
void rotate() {
|
||||
void rotate()
|
||||
{
|
||||
std::vector<std::string> copy(rows_.size());
|
||||
for (auto const &r : rows_) {
|
||||
for (auto const& r : rows_) {
|
||||
std::size_t off = copy.size();
|
||||
assert(r.size() == copy.size());
|
||||
for (auto c : r) {
|
||||
@@ -55,7 +59,8 @@ struct Picture {
|
||||
rows_ = copy;
|
||||
}
|
||||
|
||||
Hash hash(Edge edge) const {
|
||||
Hash hash(Edge edge) const
|
||||
{
|
||||
unsigned x = (edge == Edge::Right) ? rows_[0].size() - 1 : 0;
|
||||
unsigned y = (edge == Edge::Bottom) ? rows_.size() - 1 : 0;
|
||||
unsigned dx = (edge == Edge::Top || edge == Edge::Bottom) ? 1 : 0;
|
||||
@@ -74,19 +79,18 @@ struct Picture {
|
||||
|
||||
Id id() const noexcept { return id_; }
|
||||
|
||||
bool operator<(Picture const &pict) const noexcept { return id_ < pict.id_; }
|
||||
bool operator<(Picture const& pict) const noexcept { return id_ < pict.id_; }
|
||||
|
||||
bool operator==(Picture const &pict) const noexcept {
|
||||
return id_ == pict.id_;
|
||||
}
|
||||
bool operator==(Picture const& pict) const noexcept { return id_ == pict.id_; }
|
||||
|
||||
bool in_use() const noexcept { return in_use_; }
|
||||
void use() noexcept { in_use_ = true; }
|
||||
void release() noexcept { in_use_ = false; }
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Picture const &pict);
|
||||
friend std::ostream& operator<<(std::ostream& os, Picture const& pict);
|
||||
|
||||
unsigned find_monsters() {
|
||||
unsigned find_monsters()
|
||||
{
|
||||
for (unsigned r = 0; r < 4; ++r) {
|
||||
rotate();
|
||||
for (unsigned f = 0; f < 2; ++f) {
|
||||
@@ -101,9 +105,10 @@ struct Picture {
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned roughness() const {
|
||||
unsigned roughness() const
|
||||
{
|
||||
unsigned rough = 0;
|
||||
for (auto const &r : rows_) {
|
||||
for (auto const& r : rows_) {
|
||||
for (auto c : r) {
|
||||
if (c == '#') {
|
||||
++rough;
|
||||
@@ -114,7 +119,8 @@ struct Picture {
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned find_monsters1() {
|
||||
unsigned find_monsters1()
|
||||
{
|
||||
// 0 1
|
||||
// 01234567890123456789
|
||||
// #
|
||||
@@ -146,12 +152,13 @@ private:
|
||||
for (std::size_t y = 0; y <= rows_.size() - mheight; ++y) {
|
||||
for (std::string::size_type x = 0; x <= rows_[y].size() - mwidth; ++x) {
|
||||
std::size_t cy = 0;
|
||||
std::string::size_type const *cx = locs;
|
||||
std::string::size_type const* cx = locs;
|
||||
bool found = true;
|
||||
while (cy < mheight) {
|
||||
if (*cx == std::string::npos) {
|
||||
++cy;
|
||||
} else if (rows_[y + cy][x + *cx] != '#') {
|
||||
}
|
||||
else if (rows_[y + cy][x + *cx] != '#') {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
@@ -160,11 +167,12 @@ private:
|
||||
if (found) {
|
||||
++monster_count;
|
||||
std::size_t cy = 0;
|
||||
std::string::size_type const *cx = locs;
|
||||
std::string::size_type const* cx = locs;
|
||||
while (cy < mheight) {
|
||||
if (*cx == std::string::npos) {
|
||||
++cy;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
rows_[y + cy][x + *cx] = '*';
|
||||
}
|
||||
++cx;
|
||||
@@ -182,8 +190,9 @@ private:
|
||||
bool in_use_;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Picture const &pict) {
|
||||
for (auto const &r : pict.rows_) {
|
||||
std::ostream& operator<<(std::ostream& os, Picture const& pict)
|
||||
{
|
||||
for (auto const& r : pict.rows_) {
|
||||
os << r << '\n';
|
||||
}
|
||||
return os;
|
||||
@@ -193,14 +202,16 @@ using Pictures = std::map<Id, Picture>;
|
||||
using HashMap = std::multimap<Hash, Id>;
|
||||
using Array = std::map<std::pair<unsigned, unsigned>, Id>;
|
||||
|
||||
struct PictureArray {
|
||||
void add(Picture &&pic) {
|
||||
struct PictureArray
|
||||
{
|
||||
void add(Picture&& pic)
|
||||
{
|
||||
auto id = pic.id();
|
||||
auto [it, success] = pictures_.insert(std::make_pair(id, std::move(pic)));
|
||||
assert(success);
|
||||
|
||||
// Set up hash -> ID mapping
|
||||
Picture &picture = it->second;
|
||||
Picture& picture = it->second;
|
||||
for (unsigned r = 0; r < 4; ++r) {
|
||||
for (unsigned f = 0; f < 2; ++f) {
|
||||
hash_map_.insert({picture.hash(Edge::Top), picture.id()});
|
||||
@@ -210,13 +221,13 @@ struct PictureArray {
|
||||
}
|
||||
}
|
||||
|
||||
Id solve() {
|
||||
Id solve()
|
||||
{
|
||||
assert(pictures_.size() == 9 || pictures_.size() == 144);
|
||||
for (auto &kv : pictures_) {
|
||||
for (auto& kv : pictures_) {
|
||||
if (try_position(0, 0, kv.second)) {
|
||||
print_ids();
|
||||
return piece(0, 0).id() * piece(width() - 1, 0).id() *
|
||||
piece(0, height() - 1).id() *
|
||||
return piece(0, 0).id() * piece(width() - 1, 0).id() * piece(0, height() - 1).id() *
|
||||
piece(width() - 1, height() - 1).id();
|
||||
}
|
||||
}
|
||||
@@ -224,12 +235,15 @@ struct PictureArray {
|
||||
assert(false);
|
||||
return 0;
|
||||
}
|
||||
unsigned width() const noexcept {
|
||||
unsigned width() const noexcept
|
||||
{
|
||||
if (pictures_.size() == 9) {
|
||||
return 3;
|
||||
} else if (pictures_.size() == 144) {
|
||||
}
|
||||
else if (pictures_.size() == 144) {
|
||||
return 12;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
assert(false);
|
||||
}
|
||||
|
||||
@@ -238,16 +252,18 @@ struct PictureArray {
|
||||
|
||||
unsigned height() const noexcept { return width(); }
|
||||
|
||||
Picture const &piece(unsigned x, unsigned y) const {
|
||||
auto const &it = array_.find({x, y});
|
||||
Picture const& piece(unsigned x, unsigned y) const
|
||||
{
|
||||
auto const& it = array_.find({x, y});
|
||||
assert(it != array_.end());
|
||||
auto const &itp = pictures_.find(it->second);
|
||||
auto const& itp = pictures_.find(it->second);
|
||||
assert(itp != pictures_.end());
|
||||
return itp->second;
|
||||
}
|
||||
|
||||
private:
|
||||
bool try_position(unsigned x, unsigned y, Picture &pict) {
|
||||
bool try_position(unsigned x, unsigned y, Picture& pict)
|
||||
{
|
||||
if (pict.in_use()) {
|
||||
return false;
|
||||
}
|
||||
@@ -304,7 +320,8 @@ private:
|
||||
return false;
|
||||
}
|
||||
|
||||
void print_ids() const {
|
||||
void print_ids() const
|
||||
{
|
||||
for (unsigned y = 0; y < height(); ++y) {
|
||||
for (unsigned x = 0; x < width(); ++x) {
|
||||
std::cout << " " << piece(x, y).id();
|
||||
@@ -318,11 +335,12 @@ private:
|
||||
Array array_;
|
||||
};
|
||||
|
||||
Picture::Picture(PictureArray const &array) : id_(0), in_use_(false) {
|
||||
Picture::Picture(PictureArray const& array) : id_(0), in_use_(false)
|
||||
{
|
||||
for (unsigned y = 0; y < array.height(); ++y) {
|
||||
auto ybase = rows_.size();
|
||||
for (unsigned x = 0; x < array.width(); ++x) {
|
||||
auto &pict = array.piece(x, y);
|
||||
auto& pict = array.piece(x, y);
|
||||
for (auto py = 1; py < pict.rows_.size() - 1; ++py) {
|
||||
auto yidx = ybase + py - 1;
|
||||
if (rows_.size() <= yidx) {
|
||||
@@ -336,7 +354,8 @@ Picture::Picture(PictureArray const &array) : id_(0), in_use_(false) {
|
||||
assert(rows_[0].size() == array.width() * 8);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int main(void)
|
||||
{
|
||||
PictureArray pictures_;
|
||||
|
||||
std::string line;
|
||||
|
Reference in New Issue
Block a user