diff --git a/C++ProjectTemplate b/C++ProjectTemplate index c1aa29d..2e4d29a 100755 Binary files a/C++ProjectTemplate and b/C++ProjectTemplate differ diff --git a/input b/input new file mode 100644 index 0000000..41baeb5 --- /dev/null +++ b/input @@ -0,0 +1,74 @@ +.##.#.#####..##.#.#...#.##....#.######.##.#.#.##..#.#.##.#.######.#.###.## +.#.#..#####....###.####.###..##.....#####..#.#####.##...#.##.#.##....####. +........##..#.###.#.#..#..#.######..#.##.#.#####.#..##...#.#.##...###.#... +.#..#....#.......#..#...##.#.##.#..##..#.###.######.#.#.#####....#...####. +##......##.####...##.###....##.#.####..###...#..#.#...#..#.##..###.#.####. +#...#..##.######.#...##...#.#........##...#.###..#.##..##.##...#######.##. +#....#.#..###...####.....#..##.##......#.....###.##.....#.####..##..#..#.. +...##.#.##.#.#...##....#.#.###.###.##..#.##.###...#..#..##..#....#.##.##.# +#....#########.####..#....##.###.##.....###.#..##...##.#####.###..##.....# +###.######.###.#####..####.#....#.###..#..###.#.####.###..#.##...##.#..#.. +.##....#..##..#....######.##..###.#.#.#...###...#.###.##.#....#.##.#.####. +.......##...##..#..####.##........##...##.#.###.#####.####.##...####.#.... +###.#......#..#.#.#.####...#####.#.####.#.####....#.....#.######....##.##. +###...#.##.####..#..##.#.#.#...#.###..##..##.###....##.....##..#.##..#.#.. +#########.#.####..##.#.#...##.##....####..#####..#....#.#...######.###.... +#.##....###..####.#..#####.#..##.###....#.#.###..##.#....###.#.#.#.#..##.. +##.###....###..##..###..#.#..##.#.##.#..#.......#.##.######.#...#...#.#### +##.......#.....####...#...#.##.#..##.##........####..#.....#...##.#...#.## +.##..#.#....##.....##.##.#.#.###...##...#..####...#.#.#..######.#...##.... +.#....##.#.#.#..#.#.#....#....###.##.###....#.....###.#...#.##..#..####..# +..##...##..###..#####.########.##....#.#...##.##..#.#.##...##..#...##.##.# +#.#...##....#...###....#...#.##.#.##.######.##...#####...##.##...#######.. +.#.#.######.##..#.####.#.##.##.##.#...####......##.....##...####..##.####. +.###.###..##.....#####...####..##...##.##.##..##...#..#.#..##.....###.#.## +#.#.####.##.#.###.###.#..#.#....#######.###......#.##.....######.....##... +##..#.#.#..#....##.....#.###.#..##.....##############.#.#.####.##..#.##.## +#.....#.#..#.##..##..##..##...###....###..##.##...##.##....#..#######..##. +##.##..###.#..###.##.####.#.#.........##.#......#.###.#..##..#...##..#.##. +..#.###.##.####.####.#..#.###...#.#.###.#.##..#....#.#.#...#....###....### +#..#...##.#....###.#.###..#...###.##.#.######......#.##.#..#..#.###.###### +#.#...#.##..#....###....#.#.#.....########.#..##...#.#..############.##### +..####.#....#####.#.#.#....#..#.##....#..#..###.####.#....##.##...#.#.##.# +.##...#.#..#.###..#...#..#.#.#....#..#.###.##.#.####.#.###..#..#.....##..# +###..#.##...#.###.###...##..#.####....#.####......#..###...#...#.#....#.## +...#.######...#.########..######...##.....###.####.#.###...#.###.###.#.... +##.#####...###.##.#.####..#...#.#####.##.....#....####.#####.####.###..#.# +....###...#.#..#.##.#...#....#.#.#..##########.....#..##..##..##.##.##.#.. +#....#..##.#...###..####.#......########.#.......###..#...##.#.#..#.....#. +....#.......###.##.##.###.#.####.#...##.#.#..#.#.#.#.....##.#.#...#.#.#.## +###.#.##..##..####.#####.#.##.###..#.#######.##...#.#####.##.##.###..#...# +####.#.#..#..##.##..#.#.##.#.###.#...#.#..##.######.#.###.#..#..####..#..# +#......#.###.#.##...##..##...#.#..#.#.###...#..##..##.###.#.#..#.####.#..# +.###.#.#.....#.#####.....#.#..#...#...####...#.####....#..#.#...#.#.#..### +##....######.#.....##.....#.###...#..###.#.##...##..#.###.....#..#..#.#... +##..#.#.##....#.#...##.##.##.##.#.#.##..#.#..#..##.##..##..#.##..#....##.# +#.##.#.#.###...######...##.#.#.##.#.#..#...#.###..##.##.#....#..#..#.#...# +..##...#....#.##.##..##...##.......#..#####...#.#...###..#.#.##...#..#.... +#.#.....##.##.#.#.#...##.#....#...#.#.##..#.####.#.#.##..#.##.##.###....## +..#.##..#.#..##.#..##....###.##...#.##...#.#....##.###.#.#.##...##.##..#.# +.######.###........#.#.....#.#.......#.##.########.#...#.#..##.##.##..#### +#..##.....##....###.###.#.#..##..##..#.#.#.##.#.....##.###.#.##.##.#.#...# +.......###..##.##.####.#.#####.#..#...#####...#...#..#.##.####..##.##..##. +##....#####.##....#..###.####.###.###..#.#.#..#.#.##..#..###.#########.#.. +#.###..###...#.#..#.#...#.#.##.#.######.####..#..##..##.#......###.#..#... +.##..#####..#.#....###.###.##.....##..#.##....#..##.#...####...##.#....... +##.####..##...##.#...######.###.###.#.###..####.####.##..###.##.##..###### +##.#.####.#.#..#.#.###.......###......####.#.#.##.#...##..##..##.....#.#.# +.###....####.#...#.#......#####.##.##.#...###..##...##...#####....##.#..## +#..###..##....#...##.###..##.##.##########..#..##...#....##....#.......##. +###.###..#.#..#....##.#......#.###.#.#.#.#.##...#..#..##......###.##..#.#. +....##......#......#....#.##.####.####.##.###.#.##.#..#.#.#..........#.#.# +#...####...#.###..##....##..##.#####.###.#...#..#.....##.#.###.##.#...#.## +.###..##.#.###.#....#..#.###.######.##.#####..#..#.#..##.#..######.##....# +.#....###..#.#.###....##.#...###.#.#...##.#.##.##.#.#.##.#.#...#.#........ +..#.##...#....##.#..#.#.#.......#..#......#..#..#..#.#.##....#.#.#...###.. +##.#.#..##.##.#....#.#...........###....#..##.##.######.##.##..####.#..... +..##...###....###.#####..###......###.#..##...##....##.#.#.###....#..##..# +.#....#.#.#.###..##.##.#....#.##.##....#.#.##..###..##...###.#...#....##.. +#...#...####...####...#.#.#.#####....####..#..##.###.#.#....##...#.##..##. +###..#####..#.#.#...#.#.#...#...###.####.###..#...###.####.####..##.#..#.. +#...#####...##..#.###.#.#.#.#..####..##......#####....#####.####.####...## +..........#..#..#..##.#.##...##...#..#.#...#####.....#####..#...##.##.#..# +.#.##.###...#.##.#....###.#.##.##.#.####.#..##.#..#..#.#.##........#.##### +.###########.##.###.#..#.###....#......#.##.....####.####..##.....#####.#. diff --git a/main.cpp b/main.cpp index 8ab7e53..bc7699a 100644 --- a/main.cpp +++ b/main.cpp @@ -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 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::arrayorderList={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::mapelves; - 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<plannedMoves; + for (std::map::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;jfirst.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 @"<first; + e.dest=dest; + //std::cout<<"Elf @"<first<<"Plans to move NORTH"<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 @"<first; + e.dest=dest; + //std::cout<<"Elf @"<first<<"Plans to move SOUTH"<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 @"<first; + e.dest=dest; + } + //std::cout<<"Elf @"<first<<"Plans to move WEST"<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 @"<first; + e.dest=dest; + } + //std::cout<<"Elf @"<first<<"Plans to move EAST"<newElves; + bool moved=false; + for (std::map::iterator it=elves.begin();it!=elves.end();++it){ + Elf&e=elves[it->first]; + if (e.dest!=INVALID){ + e.pos=e.dest; + if (e.dest.xlowerRight.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;jlowerRight.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{originPoint+velocity,5},olc::utils::geom2d::rect{{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{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE); - return true; - } -}; + std::cout<