Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 89b66f0029
commit cdcb55aab8
  1. BIN
      C++ProjectTemplate
  2. 74
      input
  3. 293
      main.cpp
  4. 6
      smallinput
  5. 7
      testinput

Binary file not shown.

74
input

@ -0,0 +1,74 @@
.##.#.#####..##.#.#...#.##....#.######.##.#.#.##..#.#.##.#.######.#.###.##
.#.#..#####....###.####.###..##.....#####..#.#####.##...#.##.#.##....####.
........##..#.###.#.#..#..#.######..#.##.#.#####.#..##...#.#.##...###.#...
.#..#....#.......#..#...##.#.##.#..##..#.###.######.#.#.#####....#...####.
##......##.####...##.###....##.#.####..###...#..#.#...#..#.##..###.#.####.
#...#..##.######.#...##...#.#........##...#.###..#.##..##.##...#######.##.
#....#.#..###...####.....#..##.##......#.....###.##.....#.####..##..#..#..
...##.#.##.#.#...##....#.#.###.###.##..#.##.###...#..#..##..#....#.##.##.#
#....#########.####..#....##.###.##.....###.#..##...##.#####.###..##.....#
###.######.###.#####..####.#....#.###..#..###.#.####.###..#.##...##.#..#..
.##....#..##..#....######.##..###.#.#.#...###...#.###.##.#....#.##.#.####.
.......##...##..#..####.##........##...##.#.###.#####.####.##...####.#....
###.#......#..#.#.#.####...#####.#.####.#.####....#.....#.######....##.##.
###...#.##.####..#..##.#.#.#...#.###..##..##.###....##.....##..#.##..#.#..
#########.#.####..##.#.#...##.##....####..#####..#....#.#...######.###....
#.##....###..####.#..#####.#..##.###....#.#.###..##.#....###.#.#.#.#..##..
##.###....###..##..###..#.#..##.#.##.#..#.......#.##.######.#...#...#.####
##.......#.....####...#...#.##.#..##.##........####..#.....#...##.#...#.##
.##..#.#....##.....##.##.#.#.###...##...#..####...#.#.#..######.#...##....
.#....##.#.#.#..#.#.#....#....###.##.###....#.....###.#...#.##..#..####..#
..##...##..###..#####.########.##....#.#...##.##..#.#.##...##..#...##.##.#
#.#...##....#...###....#...#.##.#.##.######.##...#####...##.##...#######..
.#.#.######.##..#.####.#.##.##.##.#...####......##.....##...####..##.####.
.###.###..##.....#####...####..##...##.##.##..##...#..#.#..##.....###.#.##
#.#.####.##.#.###.###.#..#.#....#######.###......#.##.....######.....##...
##..#.#.#..#....##.....#.###.#..##.....##############.#.#.####.##..#.##.##
#.....#.#..#.##..##..##..##...###....###..##.##...##.##....#..#######..##.
##.##..###.#..###.##.####.#.#.........##.#......#.###.#..##..#...##..#.##.
..#.###.##.####.####.#..#.###...#.#.###.#.##..#....#.#.#...#....###....###
#..#...##.#....###.#.###..#...###.##.#.######......#.##.#..#..#.###.######
#.#...#.##..#....###....#.#.#.....########.#..##...#.#..############.#####
..####.#....#####.#.#.#....#..#.##....#..#..###.####.#....##.##...#.#.##.#
.##...#.#..#.###..#...#..#.#.#....#..#.###.##.#.####.#.###..#..#.....##..#
###..#.##...#.###.###...##..#.####....#.####......#..###...#...#.#....#.##
...#.######...#.########..######...##.....###.####.#.###...#.###.###.#....
##.#####...###.##.#.####..#...#.#####.##.....#....####.#####.####.###..#.#
....###...#.#..#.##.#...#....#.#.#..##########.....#..##..##..##.##.##.#..
#....#..##.#...###..####.#......########.#.......###..#...##.#.#..#.....#.
....#.......###.##.##.###.#.####.#...##.#.#..#.#.#.#.....##.#.#...#.#.#.##
###.#.##..##..####.#####.#.##.###..#.#######.##...#.#####.##.##.###..#...#
####.#.#..#..##.##..#.#.##.#.###.#...#.#..##.######.#.###.#..#..####..#..#
#......#.###.#.##...##..##...#.#..#.#.###...#..##..##.###.#.#..#.####.#..#
.###.#.#.....#.#####.....#.#..#...#...####...#.####....#..#.#...#.#.#..###
##....######.#.....##.....#.###...#..###.#.##...##..#.###.....#..#..#.#...
##..#.#.##....#.#...##.##.##.##.#.#.##..#.#..#..##.##..##..#.##..#....##.#
#.##.#.#.###...######...##.#.#.##.#.#..#...#.###..##.##.#....#..#..#.#...#
..##...#....#.##.##..##...##.......#..#####...#.#...###..#.#.##...#..#....
#.#.....##.##.#.#.#...##.#....#...#.#.##..#.####.#.#.##..#.##.##.###....##
..#.##..#.#..##.#..##....###.##...#.##...#.#....##.###.#.#.##...##.##..#.#
.######.###........#.#.....#.#.......#.##.########.#...#.#..##.##.##..####
#..##.....##....###.###.#.#..##..##..#.#.#.##.#.....##.###.#.##.##.#.#...#
.......###..##.##.####.#.#####.#..#...#####...#...#..#.##.####..##.##..##.
##....#####.##....#..###.####.###.###..#.#.#..#.#.##..#..###.#########.#..
#.###..###...#.#..#.#...#.#.##.#.######.####..#..##..##.#......###.#..#...
.##..#####..#.#....###.###.##.....##..#.##....#..##.#...####...##.#.......
##.####..##...##.#...######.###.###.#.###..####.####.##..###.##.##..######
##.#.####.#.#..#.#.###.......###......####.#.#.##.#...##..##..##.....#.#.#
.###....####.#...#.#......#####.##.##.#...###..##...##...#####....##.#..##
#..###..##....#...##.###..##.##.##########..#..##...#....##....#.......##.
###.###..#.#..#....##.#......#.###.#.#.#.#.##...#..#..##......###.##..#.#.
....##......#......#....#.##.####.####.##.###.#.##.#..#.#.#..........#.#.#
#...####...#.###..##....##..##.#####.###.#...#..#.....##.#.###.##.#...#.##
.###..##.#.###.#....#..#.###.######.##.#####..#..#.#..##.#..######.##....#
.#....###..#.#.###....##.#...###.#.#...##.#.##.##.#.#.##.#.#...#.#........
..#.##...#....##.#..#.#.#.......#..#......#..#..#..#.#.##....#.#.#...###..
##.#.#..##.##.#....#.#...........###....#..##.##.######.##.##..####.#.....
..##...###....###.#####..###......###.#..##...##....##.#.#.###....#..##..#
.#....#.#.#.###..##.##.#....#.##.##....#.#.##..###..##...###.#...#....##..
#...#...####...####...#.#.#.#####....####..#..##.###.#.#....##...#.##..##.
###..#####..#.#.#...#.#.#...#...###.####.###..#...###.####.####..##.#..#..
#...#####...##..#.###.#.#.#.#..####..##......#####....#####.####.####...##
..........#..#..#..##.#.##...##...#..#.#...#####.....#####..#...##.##.#..#
.#.##.###...#.##.#....###.#.##.##.#.####.#..##.#..#..#.#.##........#.#####
.###########.##.###.#..#.###....#......#.##.....####.####..##.....#####.#.

@ -4,86 +4,241 @@
using namespace olc;
class Example : public olc::PixelGameEngine
{
public:
Example()
{
sAppName = "Example";
}
public:
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
contact_normal = { 0, 0 };
contact_point = { 0, 0 };
const vi2d INVALID={-9999999,-99999999};
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
enum Direction{
NORTH,
EAST,
SOUTH,
WEST
};
t_hit_near = fmax(t_near.x, t_near.y);
float t_hit_far = fmin(t_far.x, t_far.y);
struct Elf{
vi2d pos;
int orderMarker=0;
std::array<Direction,4>orderList={Direction::NORTH,Direction::SOUTH,Direction::WEST,Direction::EAST};
vi2d dest=INVALID;
};
if (t_hit_far < 0) return false;
vi2d upperLeft={0,0};
vi2d lowerRight={0,0};
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
std::map<vi2d,Elf>elves;
if (t_near.x > t_near.y)
if ( 1.0f / ray_dir.x < 0)
contact_normal = { 1, 0 };
else
contact_normal = { -1, 0};
else
if ( t_near.x < t_near.y)
if ( 1.0f / ray_dir.y < 0)
contact_normal = { 0, 1 };
else
contact_normal = { 0, -1 };
return true;
int iterations=1;
int main()
{
std::ifstream file("input");
int y=0;
while (file.good()){
std::string line;
std::getline(file,line);
if (line.length()>0){
std::cout<<line<<std::endl;
for (int i=0;i<line.length();i++){
if (line[i]=='#'){
elves[{i,y}]={{i,y}};
}
}
lowerRight={(int)line.length()-1,y++};
}
}
std::cout<<elves.size()<<" elves loaded."<<std::endl;
while (true){
std::map<vi2d,vi2d>plannedMoves;
for (std::map<vi2d,Elf>::iterator it=elves.begin();it!=elves.end();++it){
Elf&e=elves[it->first];
int neighbors=0;
for (int x=-1;x<=1;x++){
for (int y=-1;y<=1;y++){
if (x!=0||y!=0){
if (elves.find({x+it->first.x,y+it->first.y})!=elves.end()){
neighbors++;
goto neighborCheck;
}
}
}
}
neighborCheck:
if (neighbors>0){
for (int j=0;j<e.orderList.size();j++){
Direction orderDir=e.orderList[j];
switch (orderDir){
case NORTH:{
if (elves.find({it->first.x,it->first.y-1})==elves.end()&&
elves.find({it->first.x-1,it->first.y-1})==elves.end()&&
elves.find({it->first.x+1,it->first.y-1})==elves.end()
){
vi2d dest={it->first.x,it->first.y-1};
if (plannedMoves.find(dest)!=plannedMoves.end()){
if (elves.find(plannedMoves[dest])==elves.end()){
std::cout<<"WARNING! Could not find Elf @"<<plannedMoves[dest]<<std::endl;
}
elves[plannedMoves[dest]].dest=INVALID;
//std::cout<<"Overlap found! Invalidated elves attempting to move to "<<plannedMoves[dest]<<std::endl;
} else {
plannedMoves[dest]=it->first;
e.dest=dest;
//std::cout<<"Elf @"<<it->first<<"Plans to move NORTH"<<std::endl;
}
goto orderComplete;
}
}break;
case SOUTH:{
if (elves.find({it->first.x,it->first.y+1})==elves.end()&&
elves.find({it->first.x-1,it->first.y+1})==elves.end()&&
elves.find({it->first.x+1,it->first.y+1})==elves.end()
){
vi2d dest={it->first.x,it->first.y+1};
if (plannedMoves.find(dest)!=plannedMoves.end()){
if (elves.find(plannedMoves[dest])==elves.end()){
std::cout<<"WARNING! Could not find Elf @"<<plannedMoves[dest]<<std::endl;
}
elves[plannedMoves[dest]].dest=INVALID;
//std::cout<<"Overlap found! Invalidated elves attempting to move to "<<plannedMoves[dest]<<std::endl;
} else {
plannedMoves[dest]=it->first;
e.dest=dest;
//std::cout<<"Elf @"<<it->first<<"Plans to move SOUTH"<<std::endl;
}
goto orderComplete;
}
}break;
case WEST:{
if (elves.find({it->first.x-1,it->first.y})==elves.end()&&
elves.find({it->first.x-1,it->first.y-1})==elves.end()&&
elves.find({it->first.x-1,it->first.y+1})==elves.end()
){
vi2d dest={it->first.x-1,it->first.y};
if (plannedMoves.find(dest)!=plannedMoves.end()){
if (elves.find(plannedMoves[dest])==elves.end()){
std::cout<<"WARNING! Could not find Elf @"<<plannedMoves[dest]<<std::endl;
}
elves[plannedMoves[dest]].dest=INVALID;
} else {
plannedMoves[dest]=it->first;
e.dest=dest;
}
//std::cout<<"Elf @"<<it->first<<"Plans to move WEST"<<std::endl;
goto orderComplete;
}
}break;
case EAST:{
if (elves.find({it->first.x+1,it->first.y})==elves.end()&&
elves.find({it->first.x+1,it->first.y-1})==elves.end()&&
elves.find({it->first.x+1,it->first.y+1})==elves.end()
){
vi2d dest={it->first.x+1,it->first.y};
if (plannedMoves.find(dest)!=plannedMoves.end()){
if (elves.find(plannedMoves[dest])==elves.end()){
std::cout<<"WARNING! Could not find Elf @"<<plannedMoves[dest]<<std::endl;
}
elves[plannedMoves[dest]].dest=INVALID;
//std::cout<<"Overlap found! Invalidated elves attempting to move to "<<plannedMoves[dest]<<std::endl;
} else {
plannedMoves[dest]=it->first;
e.dest=dest;
}
//std::cout<<"Elf @"<<it->first<<"Plans to move EAST"<<std::endl;
goto orderComplete;
}
}break;
}
}
orderComplete:;
}
}
upperLeft={0,0};
lowerRight={0,0};
std::map<vi2d,Elf>newElves;
bool moved=false;
for (std::map<vi2d,Elf>::iterator it=elves.begin();it!=elves.end();++it){
Elf&e=elves[it->first];
if (e.dest!=INVALID){
e.pos=e.dest;
if (e.dest.x<upperLeft.x){
upperLeft.x=e.dest.x;
}
if (e.dest.y<upperLeft.y){
upperLeft.y=e.dest.y;
}
if (e.dest.x>lowerRight.x){
lowerRight.x=e.dest.x;
}
if (e.dest.y>lowerRight.y){
lowerRight.y=e.dest.y;
}
e.dest=INVALID;
Direction temp=e.orderList[0];
for (int j=0;j<e.orderList.size()-1;j++){
Direction&dir=e.orderList[j];
dir=e.orderList[j+1];
}
Direction&tempDir=e.orderList[e.orderList.size()-1];
tempDir=temp;
/*std::cout<<"Next orders: ";
for (int j=0;j<e.orderList.size();j++){
std::cout<<e.orderList[j];
}
std::cout<<std::endl;*/
moved=true;
} else {
if (e.pos.x<upperLeft.x){
upperLeft.x=e.pos.x;
}
if (e.pos.y<upperLeft.y){
upperLeft.y=e.pos.y;
}
if (e.pos.x>lowerRight.x){
lowerRight.x=e.pos.x;
}
if (e.pos.y>lowerRight.y){
lowerRight.y=e.pos.y;
}
Direction temp=e.orderList[0];
for (int j=0;j<e.orderList.size()-1;j++){
Direction&dir=e.orderList[j];
dir=e.orderList[j+1];
}
Direction&tempDir=e.orderList[e.orderList.size()-1];
tempDir=temp;
/*std::cout<<"Next orders: ";
for (int j=0;j<e.orderList.size();j++){
std::cout<<e.orderList[j];
}
std::cout<<std::endl;*/
}
newElves[e.pos]=e;
}
elves=newElves;
if (!moved){
break;
}
iterations++;
}
vf2d originPoint={16,16};
bool OnUserCreate() override
{
// Called once at the start, so create things here
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
vf2d contact_point;
vf2d contact_normal;
float t_hit_near;
Clear(Pixel(64,64,255));
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
originPoint+=velocity;
DrawCircle(originPoint,5);
} else {
DrawCircle(originPoint,5,RED);
for (int y=upperLeft.y;y<=lowerRight.y;y++){
for (int x=upperLeft.x;x<=lowerRight.x;x++){
if (elves.find({x,y})==elves.end()){
std::cout<<'.';
} else {
std::cout<<'#';
}
}
DrawLine(originPoint,GetMousePos());
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE);
return true;
}
};
std::cout<<std::endl;
}
int sum=0;
for (int x=upperLeft.x;x<=lowerRight.x;x++){
for (int y=upperLeft.y;y<=lowerRight.y;y++){
if (elves.find({x,y})==elves.end()){
sum++;
}
}
}
int main()
{
Example demo;
if (demo.Construct(128, 120, 8, 8))
demo.Start();
std::cout<<"Iteration Count: "<<iterations<<std::endl;
return 0;
}

@ -0,0 +1,6 @@
.....
..##.
..#..
.....
..##.
.....

@ -0,0 +1,7 @@
....#..
..###.#
#...#.#
.#...##
#.###..
##.#.##
.#..#..
Loading…
Cancel
Save