blob: 9de484df240be0ae5ab692c6baa0edbedd5d0a85 (
plain)
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
|
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
namespace {
string
read_line(const filesystem::path& filename)
{
ifstream file{ filename };
string line;
getline(file, line);
return line;
}
bool
xorEqual(char lhs, char rhs)
{
return (lhs ^ (1 << 5)) == rhs;
}
#if 0
void
part1(string str)
{
size_t idx = 1;
while ( idx < str.length() ) {
if ( xorEqual(str[idx - 1], str[idx]) ) {
str.erase(idx - 1, 2);
idx = 1;
}
else {
++idx;
}
}
cout << "Part 1: " << str.length() << '\n';
}
#endif
void
part1_opti(string_view str)
{
string result;
for ( const auto chr: str ) {
if ( !result.empty() && xorEqual(result.back(), chr) ) {
result.pop_back();
}
else {
result.push_back(chr);
}
}
cout << "Part 1: " << result.length() << '\n';
}
void
part2(string_view str)
{
size_t min_length = str.size();
for ( auto ch = 'a'; ch != 'z'; ++ch ) {
string result;
for ( const auto chr: str ) {
if ( tolower(chr) == ch ) {
// nichts
}
else if ( !result.empty() && xorEqual(result.back(), chr) ) {
result.pop_back();
}
else {
result.push_back(chr);
}
}
min_length = min(min_length, result.length());
}
cout << "Part 2: " << min_length << '\n';
}
} // namespace
int
main()
{
auto line = read_line("data/day05.txt");
// part1(line);
part1_opti(line);
part2(line);
/*
cout << reduce("aA") << '\n';
cout << reduce("abBA") << '\n';
cout << reduce("abAB") << '\n';
cout << reduce("aabAAB") << '\n';
cout << reduce("dabAcCaCBAcCcaDA") << '\n';
*/
}
|