aboutsummaryrefslogtreecommitdiff
path: root/2018/src/day05.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-11-16 13:23:34 +0100
committerThomas Schmucker <ts@its1.de>2025-11-16 13:23:34 +0100
commit8c96639ec1f6757570510fc27f1c5fabece35eaf (patch)
tree3a2b2146b6e8a353fa4c5edd105790320cce2b26 /2018/src/day05.cpp
parentbdd35a7bee7f23c912d0c453abc263805d8d8ccf (diff)
downloadadvent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.gz
advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.tar.bz2
advent-of-code-8c96639ec1f6757570510fc27f1c5fabece35eaf.zip
aoc 2018, days 1-11
Diffstat (limited to '2018/src/day05.cpp')
-rw-r--r--2018/src/day05.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/2018/src/day05.cpp b/2018/src/day05.cpp
new file mode 100644
index 0000000..9de484d
--- /dev/null
+++ b/2018/src/day05.cpp
@@ -0,0 +1,101 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <string>
5
6using namespace std;
7
8namespace {
9
10string
11read_line(const filesystem::path& filename)
12{
13 ifstream file{ filename };
14 string line;
15 getline(file, line);
16 return line;
17}
18
19bool
20xorEqual(char lhs, char rhs)
21{
22 return (lhs ^ (1 << 5)) == rhs;
23}
24
25#if 0
26void
27part1(string str)
28{
29 size_t idx = 1;
30 while ( idx < str.length() ) {
31 if ( xorEqual(str[idx - 1], str[idx]) ) {
32 str.erase(idx - 1, 2);
33 idx = 1;
34 }
35 else {
36 ++idx;
37 }
38 }
39
40 cout << "Part 1: " << str.length() << '\n';
41}
42#endif
43
44void
45part1_opti(string_view str)
46{
47 string result;
48
49 for ( const auto chr: str ) {
50 if ( !result.empty() && xorEqual(result.back(), chr) ) {
51 result.pop_back();
52 }
53 else {
54 result.push_back(chr);
55 }
56 }
57
58 cout << "Part 1: " << result.length() << '\n';
59}
60
61void
62part2(string_view str)
63{
64 size_t min_length = str.size();
65 for ( auto ch = 'a'; ch != 'z'; ++ch ) {
66 string result;
67
68 for ( const auto chr: str ) {
69 if ( tolower(chr) == ch ) {
70 // nichts
71 }
72 else if ( !result.empty() && xorEqual(result.back(), chr) ) {
73 result.pop_back();
74 }
75 else {
76 result.push_back(chr);
77 }
78 }
79 min_length = min(min_length, result.length());
80 }
81
82 cout << "Part 2: " << min_length << '\n';
83}
84
85} // namespace
86
87int
88main()
89{
90 auto line = read_line("data/day05.txt");
91 // part1(line);
92 part1_opti(line);
93 part2(line);
94 /*
95 cout << reduce("aA") << '\n';
96 cout << reduce("abBA") << '\n';
97 cout << reduce("abAB") << '\n';
98 cout << reduce("aabAAB") << '\n';
99 cout << reduce("dabAcCaCBAcCcaDA") << '\n';
100 */
101}