-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03.cpp
89 lines (76 loc) · 1.77 KB
/
day03.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
#include <iostream>
#include <fstream>
#include <istream>
#include <map>
#include <set>
using namespace std;
class Santa {
private :
int x;
int y;
public :
Santa() : x(0), y(0) {}
inline void go_north() { ++y; }
inline void go_south() { --y; }
inline void go_east() { ++x; }
inline void go_west() { --x; }
inline int getx() const { return x; }
inline int gety() const { return y; }
};
class World {
static const char north = '^';
static const char south = 'v';
static const char east = '>';
static const char west = '<';
private :
Santa goodman;
Santa robot;
map<int, set<int>> locations;
int houses;
bool turn;
public :
World() : houses(0), turn(true) {}
void follow_instruction(istream& is) {
// present to the house at his starting location.
update_location(goodman);
update_location(robot);
char direction;
while (is.get(direction).good()) {
if (turn)
move(goodman, direction);
else
move(robot, direction);
turn = !turn;
}
}
void move(Santa& santa, const char& direction) {
if (direction == north) santa.go_north();
else if (direction == south) santa.go_south();
else if (direction == east) santa.go_east();
else if (direction == west) santa.go_west();
update_location(santa);
}
void update_location(const Santa& santa) {
if (locations[santa.getx()].insert(santa.gety()).second)
++houses;
}
int get_houses_count() const { return houses; }
};
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage : Day-03 [file]" << endl;
return 1;
}
ifstream file(argv[1]);
if (!file.good()) {
cerr << "file not found." << endl;
return 2;
}
World earth;
earth.follow_instruction(file);
cout << "With Robot, at leat one present to "
<< earth.get_houses_count()
<< " houses." << endl;
file.close();
return 0;
}