-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec23.cpp
More file actions
214 lines (163 loc) · 5.25 KB
/
Dec23.cpp
File metadata and controls
214 lines (163 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <set>
using Positions = std::set<std::pair<int,int>>;
using MapPos = std::map<std::pair<int,int>,std::pair<int,int>>;
class ElfMap{
Positions positions{};
MapPos proposed{}; //associates a proposed move to the initial position
MapPos moves{}; //associates the initial position to the final move
std::set<std::pair<int,int>> allDirections{{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
std::vector<std::pair<int,int>> north{{-1,0},{-1,-1},{-1,1}};
std::vector<std::pair<int,int>> south{{1,0},{1,-1},{1,1}};
std::vector<std::pair<int,int>> west{{0,-1},{-1,-1},{1,-1}};
std::vector<std::pair<int,int>> east{{0,1},{-1,1},{1,1}};
std::vector<std::vector<std::pair<int,int>>> directions{};
bool checkAllDirections(const std::pair<int,int>& pos) const;
void addProposed(const std::pair<int,int>&,const std::pair<int,int>&);
public:
bool anyElfMoves{false};
ElfMap(){
directions.push_back(north);
directions.push_back(south);
directions.push_back(west);
directions.push_back(east);
}
void print(int,int) const;
void readMap(std::ifstream&);
void playRound();
int getRectangle() const;
};
void ElfMap::print(int mincoord,int maxcoord) const{
for (int x=mincoord;x<=maxcoord;++x){
for (int y=mincoord;y<=maxcoord;++y){
if (positions.find({x,y})!=positions.end()){
std::cout<<"# ";
}else std::cout<<". ";
}
std::cout<<'\n';
}
std::cout<<'\n';
}
void ElfMap::readMap(std::ifstream& file){
std::string str{};
char c{};
int row{};
while (getline(file,str)){
std::stringstream ss{str};
int col{};
while (ss.get(c)){
if (c=='#') positions.insert({row,col});
++col;
}
++row;
}
}
bool ElfMap::checkAllDirections(const std::pair<int,int>& pos) const{
//RETURNS TRUE IF ALL DIRECTIONS ARE CLEAR
for (auto it=allDirections.begin();it!=allDirections.end();++it){
std::pair<int,int> posToCheck{pos};
posToCheck.first += it->first;
posToCheck.second += it->second;
if (positions.find(posToCheck)!=positions.end()){
return false;
}
}
return true;
}
void ElfMap::addProposed(const std::pair<int,int>& prop,const std::pair<int,int>& orig){
auto found = proposed.find(prop);
if (found==proposed.end()){
proposed.insert({prop,orig});
moves.insert({orig,prop});
}else{
moves.insert({orig,orig});
moves.find(found->second)->second = found->second;
}
}
void ElfMap::playRound(){
proposed.clear();
moves.clear();
anyElfMoves = false;
for (auto it=positions.begin();it!=positions.end();++it){
auto cur_pos = *it;
if (checkAllDirections(cur_pos)){
moves.insert({cur_pos,cur_pos});
}else{
for (int i=0;i<4;++i){
auto dirset = directions[i];
bool cont{true};
for (auto dir: dirset){
std::pair<int,int> posToCheck{cur_pos};
posToCheck.first += dir.first;
posToCheck.second += dir.second;
if (positions.find(posToCheck)!=positions.end()){
cont = false;
}
if (!cont) break;
}
if (cont){
//if reach here, all 3 directions are clear
std::pair<int,int> propMove{cur_pos};
propMove.first += dirset[0].first;
propMove.second += dirset[0].second;
addProposed(propMove,cur_pos);
break;
}
}
}
//add positions that cannot move in any of the 4 directions
if (moves.find(cur_pos)==moves.end()){
moves.insert({cur_pos,cur_pos});
}
}
auto top_dir = directions.front();
directions.erase(directions.begin());
directions.push_back(top_dir);
positions.clear();
for (auto move: moves){
positions.insert(move.second);
if (move.first!=move.second) anyElfMoves = true;
}
}
int ElfMap::getRectangle() const{
int minx{INT_MAX};
int maxx{INT_MIN};
int miny{INT_MAX};
int maxy{INT_MIN};
for (auto pos: positions){
minx = std::min(minx,pos.first);
maxx = std::max(maxx,pos.first);
miny = std::min(miny,pos.second);
maxy = std::max(maxy,pos.second);
}
return (maxx-minx+1)*(maxy-miny+1)-positions.size();
}
int main(){
bool part_one{false};
std::ifstream file("data23.txt");
ElfMap elfmap{};
elfmap.readMap(file);
if (part_one){
for (int round=1;round<=10;++round){
elfmap.playRound();
}
std::cout<<elfmap.getRectangle()<<'\n';
}else{
int round{1};
while (true){
elfmap.playRound();
if (!elfmap.anyElfMoves){
std::cout<<"Stop at round "<<round<<'\n';
break;
}
++round;
}
}
//elfmap.print(0,50);
return 0;
}