-
Notifications
You must be signed in to change notification settings - Fork 1
/
firefly.cpp
174 lines (139 loc) · 3.6 KB
/
firefly.cpp
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
// Jeff Ruhl
// Lampyridae Teleportae
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <exception>
#include <cmath>
using namespace std;
class Exception : std::exception
{
const char* _what;
public:
Exception( const char* what ) : _what( what ) {}
const char* what() const throw() { return _what; }
};
template <typename T>
struct Option
{
public:
Option() {}
bool is_empty() { return _is_empty; }
T& get() {
if ( _is_empty ) {
throw Exception("No value");
}
return _val;
}
T& operator*() {
return get();
}
protected:
bool _is_empty;
T _val;
};
template <typename T>
struct Some : public Option<T>
{
Some( const T& val ) {
this->_is_empty = false;
this->_val = val;
}
};
template <typename T>
struct None : public Option<T>
{
None() {
this->_is_empty = true;
}
};
struct Vector
{
float x, y;
bool operator==( const Vector& other ) const {
return (this->x == other.x && this->y == other.y);
}
bool operator!=( const Vector& other ) const {
return !operator==( other );
}
Vector operator+( const Vector& other ) const {
Vector result = {this->x + other.x, this->y + other.y};
return result;
}
Vector operator-( const Vector& other ) const {
Vector result = {this->x - other.x, this->y - other.y};
return result;
}
Vector operator*( float scalar ) const {
Vector result = {this->x * scalar, this->y * scalar};
return result;
}
void normalize() {
float length = get_length();
x /= length;
y /= length;
}
float get_length() const {
return sqrt( x * x + y * y );
}
};
istream& operator>>( istream &in, Vector& vector ) {
in >> vector.x;
in >> vector.y;
return in;
}
typedef vector<Vector> Vectors;
Option<Vector> catch_firefly( float, Vector, const Vectors& );
int main( int argc, char const *argv[] ) {
ifstream fin("firefly.in");
if ( fin ) {
ofstream fout("firefly.out");
float max_shoe_distance = 0;
Vector chaser_start_position = {0, 0};
Vectors firefly_positions;
Vector firefly_position = {0, 0};
Vector end_of_input = {-1, -1};
int firefly_count = 1;
fin >> max_shoe_distance;
while ( 0 != max_shoe_distance ) {
fin >> chaser_start_position;
fin >> firefly_position;
while ( firefly_position != end_of_input ) {
firefly_positions.push_back( firefly_position );
fin >> firefly_position;
}
auto result = catch_firefly( max_shoe_distance, chaser_start_position, firefly_positions );
if ( result.is_empty() ) {
fout << "Firefly " << firefly_count << " not caught" << endl;
}
else {
Vector firefly_pos = *result;
fout << "Firefly " << firefly_count << " caught at (" << firefly_pos.x << "," << firefly_pos.y << ")" << endl;
}
firefly_count++;
firefly_positions.clear();
fin >> max_shoe_distance;
}
fout.close();
fin.close();
}
else {
cout << "Could not open 'firefly.in'" << endl;
}
}
Option<Vector> catch_firefly( float max_shoe_distance, Vector chaser_position, const Vectors& firefly_positions ) {
Option<Vector> result = None<Vector>();
for ( auto firefly_pos = firefly_positions.begin(); firefly_pos != firefly_positions.end(); ++firefly_pos ) {
Vector space_from_firefly = *firefly_pos - chaser_position;
if ( space_from_firefly.get_length() <= max_shoe_distance + 1 ) {
result = Some<Vector>( *firefly_pos );
break;
}
else {
space_from_firefly.normalize();
chaser_position = chaser_position + space_from_firefly * max_shoe_distance;
}
}
return result;
}