aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day21.cpp
blob: 7e8fed781299d3ae341dcfbc11d9dd6f84bfa3e8 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <ranges>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

namespace {

void
swap_at_pos(string& str, size_t x, size_t y)
{
	if ( str.empty() ) {
		return;
	}

	swap(str.at(x), str.at(y));
}

void
swap_letter(string& str, char x, char y)
{
	if ( str.empty() ) {
		return;
	}

	for ( auto& chr: str ) {
		if ( chr == x ) {
			chr = y;
		}
		else if ( chr == y ) {
			chr = x;
		}
	}
}

void
rotate_left(string& str, size_t n)
{
	if ( str.empty() ) {
		return;
	}

	ranges::rotate(str, str.begin() + static_cast<int>(n % str.size()));
}

void
rotate_right(string& str, size_t n)
{
	if ( str.empty() ) {
		return;
	}

	rotate_left(str, str.size() - n);
}

void
rotate(string& str, char chr)
{
	if ( str.empty() ) {
		return;
	}

	const auto idx = str.find(chr);
	if ( idx == string::npos ) {
		return;
	}

	const auto steps = 1 + idx + (idx >= 4 ? 1 : 0);
	rotate_right(str, steps % str.size());
}

void
reverse(string& str, size_t x, size_t y)
{
	if ( str.empty() ) {
		return;
	}

	if ( x > y ) {
		swap(x, y);
	}

	reverse(str.begin() + static_cast<string::difference_type>(x),
	        str.begin() + static_cast<string::difference_type>(y) + 1);
}

void
move(string& str, size_t x, size_t y)
{
	if ( str.empty() ) {
		return;
	}

	const auto chr = str.at(x);

	str.erase(x, 1);
	str.insert(y, 1, chr);
}

vector<string>
readLines(const filesystem::path& filename)
{
	ifstream       file{ filename };
	vector<string> lines;

	for ( string line; getline(file, line); ) {
		lines.emplace_back(line);
	}

	return lines;
}

vector<string>
split(const string& line, char sep = ' ')
{
	vector<string> words;
	stringstream   input{ line };

	for ( string word; getline(input, word, sep); ) {
		words.emplace_back(word);
	}

	return words;
}

void
part1(const vector<string>& lines, string str)
{
	for ( const auto& line: lines ) {
		auto words = split(line);

		if ( line.starts_with("swap position") ) {
			swap_at_pos(str, stoull(words.at(2)), stoull(words.at(5)));
		}
		else if ( line.starts_with("swap letter") ) {
			swap_letter(str, words.at(2).at(0), words.at(5).at(0));
		}
		else if ( line.starts_with("rotate left") ) {
			rotate_left(str, stoull(words.at(2)));
		}
		else if ( line.starts_with("rotate right") ) {
			rotate_right(str, stoull(words.at(2)));
		}
		else if ( line.starts_with("rotate based") ) {
			rotate(str, words.at(6).at(0));
		}
		else if ( line.starts_with("reverse") ) {
			reverse(str, stoull(words.at(2)), stoull(words.at(4)));
		}
		else if ( line.starts_with("move") ) {
			move(str, stoull(words.at(2)), stoull(words.at(5)));
		}
		else {
			cerr << "unknown command: " << line << '\n';
			return;
		}
	}
	cout << str << '\n';
}

void
part2(const vector<string>& lines, string str)
{
	for ( const auto& line: ranges::reverse_view(lines) ) {
		auto words = split(line);

		if ( line.starts_with("swap position") ) {
			swap_at_pos(str, stoull(words.at(2)), stoull(words.at(5)));
		}
		else if ( line.starts_with("swap letter") ) {
			swap_letter(str, words.at(2).at(0), words.at(5).at(0));
		}
		else if ( line.starts_with("rotate left") ) {
			rotate_right(str, stoull(words.at(2)));
		}
		else if ( line.starts_with("rotate right") ) {
			rotate_left(str, stoull(words.at(2)));
		}
		else if ( line.starts_with("rotate based") ) {
			auto chr = words.at(6).at(0);
			for ( size_t i = 0; i != str.size(); ++i ) {
				string tmp = str;
				rotate_left(tmp, i);

				auto idx   = tmp.find(chr);
				auto steps = (1 + idx + (idx >= 4 ? 1 : 0));

				if ( ((steps - i + str.size()) % str.size()) == 0 ) {
					str = tmp;
					break;
				}
			}
		}
		else if ( line.starts_with("reverse") ) {
			reverse(str, stoull(words.at(2)), stoull(words.at(4)));
		}
		else if ( line.starts_with("move") ) {
			move(str, stoull(words.at(5)), stoull(words.at(2)));
		}
		else {
			cerr << "unknown command: " << line << '\n';
			return;
		}
	}
	cout << str << '\n';
}

} // namespace

int
main()
{
	auto lines = readLines("data/day21.txt");

	part1(lines, "abcdefgh");
	part2(lines, "fbgdceah");
}