-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1601_city_blocks.cpp
130 lines (122 loc) Β· 4.56 KB
/
aoc1601_city_blocks.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
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
#include "sstream"
#include "vector"
int if_exists(std::pair<int, int> pair, \
std::vector< std::pair<int, int> > path);
int main()
{
std::vector< std::pair <int, int> > path;
std::pair<int, int> crossed;
std::string src;
int heading, counter, been, x, y;
heading = counter = x = y = 0;
while (std::cin >> src)
{
std::stringstream ss(src);
char c;
int d;
ss >> c >> d;
if (c == 'L') heading -= 1;
else heading += 1;
if (heading > 4) heading = 1;
if (heading < 1) heading = 4;
if (heading == 1)
{
if (!counter)
{
for (int i = x; i < x + d; i++)
{
std::pair<int, int> pos = std::make_pair(i, y);
been = if_exists(pos, path);
if (!been) path.push_back(pos);
else
{
crossed = pos;
counter++;
}
}
}
x += d;
}
if (heading == 2)
{
if (!counter)
{
for (int i = y; i > y - d; i--)
{
std::pair<int, int> pos = std::make_pair(x, i);
been = if_exists(pos, path);
if (!been) path.push_back(pos);
else
{
crossed = pos;
counter++;
}
}
}
y -= d;
}
if (heading == 3)
{
if (!counter)
{
for (int i = x; i > x - d; i--)
{
std::pair<int, int> pos = std::make_pair(i, y);
been = if_exists(pos, path);
if (!been) path.push_back(pos);
else
{
crossed = pos;
counter++;
}
}
}
x -= d;
}
if (heading == 4)
{
if (!counter)
{
for (int i = y; i < y + d; i++)
{
std::pair<int, int> pos = std::make_pair(x, i);
been = if_exists(pos, path);
if (!been) path.push_back(pos);
else
{
crossed = pos;
counter++;
}
}
}
y += d;
}
}
if (x < 0) x = -x;
if (y < 0) y = -y;
if (crossed.first < 0) crossed.first *= -1;
if (crossed.second < 0) crossed.second *= -1;
std::cout
<< "Star 1 : " << x + y << '\n'
<< "Star 2 : " << crossed.first + crossed.second << '\n';
}
int if_exists(std::pair<int, int> pair, \
std::vector< std::pair<int, int> > path)
{
if (std::find(path.begin(), path.end(), pair) != path.end())
return 1;
return 0;
}