-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec14.cpp
More file actions
201 lines (169 loc) · 4.46 KB
/
Dec14.cpp
File metadata and controls
201 lines (169 loc) · 4.46 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
char convert(int i){
switch (i){
case 0: return '.';
case 1: return '#';
case 2: return 'o';
default: return '?';
}
}
class SandMap{
const int height{};
const int width{};
const int startCol{};
const int dropCol{};
std::vector<std::vector<int>> map{}; //MAP: 0-AIR 1-WALL 2-SAND
public:
SandMap(int h,int w,int sC,int dC):
height{h},width{w},startCol{sC},dropCol{dC},
map{std::vector<std::vector<int>> (height,std::vector<int>(width,0))}
{
}
void print() const;
void updateMap(int col0,int row0,int col1,int row1);
bool dropSand_part1();
bool dropSand_part2();
};
void SandMap::print() const {
for (auto it=map.begin();it<map.end();++it){
for (auto itt=(*it).begin();itt<(*it).end();++itt){
std::cout<<convert(*itt)<<' ';
}
std::cout<<'\n';
}
std::cout<<'\n';
}
void SandMap::updateMap(int col0,int row0,int col1,int row1){
if (col0==col1){
int colIndex{col0-startCol};
for (int row=std::min(row0,row1);row<=std::max(row0,row1);++row){
map[row][colIndex] = 1;
}
}else{
for (int col=std::min(col0,col1);col<=std::max(col0,col1);++col){
int colIndex = col-startCol;
map[row0][colIndex] = 1;
}
}
}
bool SandMap::dropSand_part1(){
//TRUE IF SAND DIDN'T FALL OFF THE EDGE
int sandCol{dropCol};
int sandRow{0};
while(true){
if (sandRow<map.size()-1){
if (map[sandRow+1][sandCol]==0) ++sandRow;
else if(sandCol>0 && map[sandRow+1][sandCol-1]==0){
++sandRow;
--sandCol;
}else if(sandCol<map[0].size()-1 && map[sandRow+1][sandCol+1]==0){
++sandRow;
++sandCol;
}else{
map[sandRow][sandCol] = 2;
return true;
}
}else{
return false;
}
}
return true;
}
bool SandMap::dropSand_part2(){
//TRUE IF SAND DIDN'T STOP AT THE FIRST STEP
int sandCol{dropCol};
int sandRow{0};
while(true){
if (map[sandRow+1][sandCol]==0) ++sandRow;
else if(sandCol>0 && map[sandRow+1][sandCol-1]==0){
++sandRow;
--sandCol;
}else if(sandCol<map[0].size()-1 && map[sandRow+1][sandCol+1]==0){
++sandRow;
++sandCol;
}else if(sandRow==0){
map[sandRow][sandCol] = 2;
return false;
}else{
map[sandRow][sandCol] = 2;
return true;
}
}
return true;
}
int main(){
std::ifstream file("data14.txt");
std::string str{};
char dummy{};
int row0{};
int col0{};
int row1{};
int col1{};
constexpr bool part2{false};
//FIND SIZE OF THE PROBLEM
int minCol{INT_MAX};
int maxCol{0};
int maxRow{0};
while (getline(file,str)){
std::stringstream ss{str};
ss>>col0>>dummy>>row0;
minCol = std::min(minCol,col0);
maxCol = std::max(maxCol,col0);
maxRow = std::max(maxRow,row0);
while (ss.peek()!=EOF){
ss>>dummy>>dummy;
ss>>col0>>dummy>>row0;
minCol = std::min(minCol,col0);
maxCol = std::max(maxCol,col0);
maxRow = std::max(maxRow,row0);
}
}
//RESET FILE TO BEGINNING
file.clear();
file.seekg(0);
//CREATE SANDMAP
int height{};
int width{};
int startCol{};
int dropCol{};
height = maxRow+3;
width = height*2+1;
startCol = 500-height;
dropCol = 500-startCol;
SandMap sand{height,width,startCol,dropCol};
while (getline(file,str)){
std::stringstream ss{str};
ss>>col0>>dummy>>row0;
while (ss.peek()!=EOF){
ss>>dummy>>dummy;
ss>>col1>>dummy>>row1;
sand.updateMap(col0,row0,col1,row1);
col0 = col1;
row0 = row1;
}
}
if (part2){
col0=startCol;
col1=startCol+width;
row0=height-1;
row1=height-1;
sand.updateMap(col0,row0,col1,row1);
}
int iter{0};
if (part2){
do {
++iter;
} while(sand.dropSand_part2());
}else{
while(sand.dropSand_part1()){
++iter;
};
}
std::cout<<iter<<'\n';
//sand.print();
return 0;
}