-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.cpp
More file actions
123 lines (109 loc) · 4.48 KB
/
Copy pathday15.cpp
File metadata and controls
123 lines (109 loc) · 4.48 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "utils/coordinate.h"
#define LOG(x) std::cout << x << std::endl;
const std::string inputFilePath = "inputs/day15.txt";
struct Sensor{
Coordinate position;
int rangeClosestBeacon;
};
std::vector<Sensor> sensors;
bool vectorContainsBeacon(std::vector<Coordinate> beacons, Coordinate beacon){
bool found = false;
for(int i=0; i < beacons.size();i++){
if (beacons[i] == beacon){
found = true;
}
}
return found;
}
int main(){
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::string line;
std::vector<Coordinate> beacons;
// Parseinput, get sensor position + closest beacon range
int minX = INT_MAX;
int maxX = INT_MIN;
while(std::getline(ifs, line)){
Sensor s;
s.position.x = std::stoi(line.substr(line.find("x=") + 2, line.find(",")));
s.position.y = std::stoi(line.substr(line.find("y=") + 2, line.find(":")));
Coordinate beaconPos;
line = line.substr(line.find("beacon"));
beaconPos.x = std::stoi(line.substr(line.find("x=") + 2, line.find(",")));
beaconPos.y = std::stoi(line.substr(line.find("y=") + 2));
s.rangeClosestBeacon = s.position.distance(beaconPos);
sensors.push_back(s);
if (!vectorContainsBeacon(beacons, beaconPos)){
beacons.push_back(beaconPos);
}
// Set row checking boundaries for Ex 1
minX = std::min(s.position.x - s.rangeClosestBeacon,beaconPos.x) < minX ? std::min(s.position.x + s.rangeClosestBeacon,beaconPos.x) : minX;
maxX = std::max(s.position.x + s.rangeClosestBeacon,beaconPos.x) > maxX ? std::max(s.position.x + s.rangeClosestBeacon,beaconPos.x) : maxX;
}
// Ex 1 - for each x in row, check if within range of any of the sesnsors and if not, count
const int ROW_TO_CHECK = 2000000;
int positionsThatCannotContainBeacon = 0;
for(int x = minX; x <= maxX; x++){
if (!vectorContainsBeacon(beacons, {x,ROW_TO_CHECK})){
Coordinate current = {x, ROW_TO_CHECK};
for(int i=0; i < sensors.size(); i++){
if (current.distance(sensors[i].position) <= sensors[i].rangeClosestBeacon){
positionsThatCannotContainBeacon++;
break;
}
}
}
}
// Ex 2 - Check the borders of every sensor's range (Only place a beacon could be)
uint64_t tuningFrequency = 0;
const uint64_t FREQ_RANGE= 4000000;
bool found = false;
for(int i=0; i < sensors.size(); i++){
Sensor s = sensors[i];
int yOffset = 0;
for(int x=s.position.x - s.rangeClosestBeacon - 1; x <= s.position.x + s.rangeClosestBeacon + 1; x++){
Coordinate pointToCheck1 = {x, s.position.y + yOffset};
Coordinate pointToCheck2 = {x, s.position.y - yOffset};
if (pointToCheck1.inRange({0,0},{FREQ_RANGE, FREQ_RANGE})){
bool canContainIt = true;
for(int j=0; j < sensors.size(); j++){
if (pointToCheck1.distance(sensors[j].position) <= sensors[j].rangeClosestBeacon){
canContainIt = false;
break;
}
}
if (canContainIt){
tuningFrequency = (uint64_t)4000000*pointToCheck1.x + pointToCheck1.y;
found = true;
break;
}
}
if (pointToCheck2.inRange({0,0},{FREQ_RANGE, FREQ_RANGE}) && (pointToCheck2 != pointToCheck1)){
bool canContainIt = true;
for(int j=0; j < sensors.size(); j++){
if (pointToCheck2.distance(sensors[j].position) <= sensors[j].rangeClosestBeacon){
canContainIt = false;
break;
}
}
if (canContainIt){
tuningFrequency = 4000000*pointToCheck2.x + pointToCheck2.y;
found = true;
break;
}
}
if (x < s.position.x){ // Go up until reaching the sensor origin and then go down, to cover the whole perimteter
yOffset++;
}
else{
yOffset--;
}
}
}
std::cout << "--Ex1 Output: " << positionsThatCannotContainBeacon << std::endl;
std::cout << "--Ex2 Output: " << tuningFrequency << std::endl;
return 0;
}