-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2005_boarding.cpp
114 lines (94 loc) · 3.09 KB
/
aoc2005_boarding.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
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
#include "sstream"
#include "vector"
int find_missing(std::vector<int> a);
int find_highest(std::vector<int> a);
int convert_seat_id(std::string s);
void sort_id(std::vector<int> a);
// DRIVE
int main()
{
std::vector<int> all_seats;
std::string s;
int max, pid;
max = 0;
while (std::cin >> s)
{
pid = convert_seat_id(s);
all_seats.push_back(pid);
}
max = find_highest(all_seats);
pid = find_missing(all_seats);
std::cout << "Star 1 : " << max << std::endl;
std::cout << "Star 2 : " << pid << std::endl;
return (0);
}
//
int convert_seat_id(std::string s)
{
int lo, hi, le, ri;
std::stringstream ss(s);
char c;
lo = le = 0;
hi = 127;
ri = 7;
while (ss >> c)
{
if (c == 'F') hi -= (hi - lo + 1) / 2;
if (c == 'B') lo += (hi - lo + 1) / 2;
if (c == 'L') ri -= (ri - le + 1) / 2;
if (c == 'R') le += (ri - le + 1) / 2;
}
return (8 * lo + ri);
}
int find_missing(std::vector<int> a)
{
int i = -1;
//sort_id(a); // no need
while (++i < (int) a.size() - 2)
{
if ((std::find(a.begin(), a.end(), 1 + a[i]) == a.end()) &&
std::find(a.begin(), a.end(), 2 + a[i]) != a.end())
break;
}
return (1 + a[i]);
}
int find_highest(std::vector<int> a)
{
int max = 0;
int i = -1;
while (++i < (int) a.size())
{
if (max < a[i]) max = a[i];
}
return (max);
}
void sort_id(std::vector<int> a)
{
int flag, temp, i;
i = 0;
while (i < (int) a.size() - 1)
{
flag = 0;
if (a[i] < a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
flag++;
}
if (!flag) break;
}
}