2048.cpp
This commit is contained in:
71
puzzles/training/2048.cpp
Normal file
71
puzzles/training/2048.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
VIM: let b:cf5build="clang -std=c++20 -O2 -pthread -lstdc++ -I. {SRC} -o {OUT}"
|
||||
VIM: let b:cf5run="{OUT}"
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <exception>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
void print(const std::vector<int>& row)
|
||||
{
|
||||
for (auto i : row){
|
||||
std::cout << i << ',';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
void push(std::vector<int> row)
|
||||
{
|
||||
print(row);
|
||||
auto q = row.begin();
|
||||
auto pe = row.end();
|
||||
for (auto p = row.begin()+1; p != pe; ++p){
|
||||
if (*p==0){
|
||||
continue;
|
||||
}
|
||||
else if (*q == 0){
|
||||
*q+=*p;
|
||||
*p = 0;
|
||||
}
|
||||
else if (*q == *p){
|
||||
*q+=*p;
|
||||
*p = 0;
|
||||
++q;
|
||||
}
|
||||
else {
|
||||
*++q = *p;
|
||||
if (*q != *p){
|
||||
*p = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
print(row);
|
||||
}
|
||||
|
||||
int main ( void )
|
||||
{try{
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
|
||||
push({4, 0, 4, 8});
|
||||
|
||||
//......
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> seconds = end - begin;
|
||||
std::cout << "Time: " << seconds.count() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
catch ( const std::exception& e )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||
return 2;
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< "unknown exception." << std::endl;
|
||||
return 1;
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user