-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc1508_matchsticks.cpp
100 lines (88 loc) · 2.82 KB
/
aoc1508_matchsticks.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
/* ************************************************************************** */
/* */
/* \\ /`/`` */
/* ~\o o_ 0 0\ */
/* / \__) (u ); _ _ */
/* / \/ \/ / \ \/ \/ \ */
/* /( . . ) ( )\ */
/* / \_____/ \_______/ \ */
/* [] [] [[] [[] *. */
/* []] []] [[] [[] */
/* */
/* ****************************************************************** nuo *** */
#include "iostream"
int newly_encoded_string(std::string s);
int characters_in_memory(std::string s);
// DRIVE
int main()
{
int length_minus_memory;
int encode_minus_length;
int len, mem, enc;
std::string s;
len = mem = enc = 0;
while (std::cin >> s)
{
len += (int) s.length();
mem += characters_in_memory(s);
enc += newly_encoded_string(s);
}
length_minus_memory = len - mem;
encode_minus_length = enc - len;
std::cout << "Star 1 : " << length_minus_memory << std::endl;
std::cout << "Star 2 : " << encode_minus_length << std::endl;
return (0);
}
//
int newly_encoded_string(std::string s)
{
int i, encoded;
encoded = i = 0;
while (i < (int) s.length())
{
if (s[i] == '\"')
{
if (!i || i == (int) s.length() - 1)
encoded += 3;
else
encoded += 2;
i++;
}
else if (s[i] == '\\')
{
i++;
encoded += 2;
}
else
{
i++;
encoded += 1;
}
}
return (encoded);
}
int characters_in_memory(std::string s)
{
int i, memory;
memory = i = 0;
while (i < (int) s.length())
{
if (s[i] == '\"')
i++;
else if (s[i] == '\\')
{
i++;
if (s[i] == 'x')
i += 3;
else
i++;
memory++;
}
else
{
i++;
memory++;
}
}
return (memory);
}