-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec15.cpp
More file actions
177 lines (136 loc) · 3.57 KB
/
Dec15.cpp
File metadata and controls
177 lines (136 loc) · 3.57 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <set>
class Sensor{
const int x_pos{};
const int y_pos{};
const int x_bea{};
const int y_bea{};
const int dist{};
public:
Sensor(int x,int y,int x_s,int y_s):
x_pos{x},y_pos{y},
x_bea{x_s},y_bea{y_s},
dist{std::abs(x_pos-x_bea)+std::abs(y_pos-y_bea)}
{
}
int computeDist(int x,int y){
return std::abs(x_pos-x)+std::abs(y_pos-y);
}
friend class MapSensor;
};
class MapSensor{
public:
std::vector<Sensor> sensors{};
int xmin{};
int xmax{};
int ymin{};
int ymax{};
int partOne();
long long partTwo();
bool checkPoint(int x,int y);
};
int MapSensor::partOne(){
std::set<int> impossible_x{};
for (int x=10*xmin;x<=10*xmax;++x){
for (Sensor s: sensors){
if (s.computeDist(x,ymin)<=s.dist && (x!=s.x_bea || ymin!=s.y_bea)){
impossible_x.insert(x);
break;
}
}
}
return impossible_x.size();
}
long long MapSensor::partTwo(){
std::vector<std::vector<int>> direction {{1,-1},{-1,-1},{1,1},{-1,1}};
int xp{};
int yp{};
for (Sensor s: sensors){
int yUp = s.y_pos + s.dist + 1;
int yDown = s.y_pos - s.dist - 1;
int xp_start{s.x_pos};
std::vector<int> yp_start{yUp,yUp,yDown,yDown};
bool found{false};
for (int j=0;j<4;++j){
if (!found){
xp = xp_start;
yp = yp_start[j];
while (yp>=ymin && yp<=ymax && xp>=xmin && xp<=xmax){
bool res = checkPoint(xp,yp);
if (res){
found = true;
break;
}
xp += direction[j][0];
yp += direction[j][1];
}
}
}
if (found) break;
}
return (4000000*static_cast<long long>(xp)+yp);
}
bool MapSensor::checkPoint(int x,int y){
//RETURNS TRUE IF POINT CAN CONTAIN BEACON
if (y<ymin || y>ymax || x<xmin || x>xmax) return false;
for (Sensor s: sensors){
if (s.computeDist(x,y)<=s.dist) return false;
}
return true;
}
Sensor readLine(std::string str,int& x_min,int& x_max){
std::string dummys{};
char dummyc{};
std::stringstream ss{str};
ss>>dummys>>dummys;
ss>>dummyc>>dummyc;
int x_pos{};
ss>>x_pos;
x_min = std::min(x_min,x_pos);
x_max = std::max(x_max,x_pos);
ss>>dummyc>>dummyc>>dummyc;
int y_pos{};
ss>>y_pos;
ss>>dummyc>>dummys>>dummys>>dummys>>dummys;
ss>>dummyc>>dummyc;
int x_bea{};
ss>>x_bea;
x_min = std::min(x_min,x_bea);
x_max = std::max(x_max,x_bea);
ss>>dummyc>>dummyc>>dummyc;
int y_bea{};
ss>>y_bea;
return Sensor(x_pos,y_pos,x_bea,y_bea);
}
int main(){
bool part_one{false};
std::ifstream file("data15.txt");
std::string str{};
MapSensor map{};
int x_min{};
int x_max{};
while (getline(file,str)){
map.sensors.push_back(readLine(str,x_min,x_max));
}
if (part_one){
//PART ONE
constexpr int y_index{2000000};
map.ymin = y_index;
map.ymax = y_index;
map.xmin = x_min;
map.xmax = x_max;
std::cout<<"Part 1 "<<map.partOne()<<'\n';
}else{
//PART TWO
map.xmin = 0;
map.ymin = 0;
map.xmax = 4000000;
map.ymax = 4000000;
std::cout<<"Part 2 "<<map.partTwo()<<'\n';
}
return 0;
}