aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day21.cpp
blob: 9dd166a347c8724d4b07da0193059db14fbf43ae (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
#undef NDEBUG
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using pos_type = tuple<int, int>;
using map_type = map<char, pos_type>;

vector<string>
read_file(string_view filename)
{
	fstream        input{ filename };
	vector<string> lines;

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

	return lines;
}

bool
is_valid_movement(pos_type start, const pos_type dest, string_view movement, const pos_type avoid)
{
	static const map_type directions = {
		{ '^', { 0, -1 } },
		{ 'v', { 0, 1 } },
		{ '<', { -1, 0 } },
		{ '>', { 1, 0 } }
	};

	for ( const auto key: movement ) {
		const auto [dx, dy] = directions.at(key);

		get<0>(start) += dx;
		get<1>(start) += dy;

		if ( start == avoid ) {
			return false;
		}
	}

	assert(start == dest); // NOLINT

	return true;
}

vector<string>
get_movements(const pos_type start, const pos_type end, const pos_type avoid)
{
	if ( start == end ) {
		return { "A" };
	}

	const auto [start_x, start_y] = start;
	const auto [end_x, end_y]     = end;

	string movement;

	const auto delta_y = end_y - start_y;
	if ( delta_y < 0 ) {
		movement.append(size_t(-delta_y), '^');
	}
	else if ( delta_y > 0 ) {
		movement.append(size_t(delta_y), 'v');
	}

	const auto delta_x = end_x - start_x;
	if ( delta_x < 0 ) {
		movement.append(size_t(-delta_x), '<');
	}
	else if ( delta_x > 0 ) {
		movement.append(size_t(delta_x), '>');
	}

	assert(!movement.empty()); // NOLINT

	vector<string> movements;

	ranges::sort(movement);
	do {
		if ( is_valid_movement(start, end, movement, avoid) ) {
			movements.emplace_back(movement + 'A');
		}
	} while ( next_permutation(movement.begin(), movement.end()) );

	assert(!movements.empty()); // NOLINT

	return movements;
}

size_t
find_rec(string_view keys, const int depth, const int max_depth)
{
	static const map_type keypad_door{
		{ '7', { 0, 0 } },
		{ '8', { 1, 0 } },
		{ '9', { 2, 0 } },

		{ '4', { 0, 1 } },
		{ '5', { 1, 1 } },
		{ '6', { 2, 1 } },

		{ '1', { 0, 2 } },
		{ '2', { 1, 2 } },
		{ '3', { 2, 2 } },

		{ '0', { 1, 3 } },
		{ 'A', { 2, 3 } },
	};

	static const map_type keypad_robot{
		{ '^', { 1, 0 } },
		{ 'A', { 2, 0 } },

		{ '<', { 0, 1 } },
		{ 'v', { 1, 1 } },
		{ '>', { 2, 1 } }
	};

	static map<tuple<string, int, int>, size_t> cache;

	if ( cache.contains({ string(keys), depth, max_depth }) ) {
		return cache.at({ string(keys), depth, max_depth });
	}

	const auto& keypad = depth == 0 ? keypad_door : keypad_robot;
	const auto& avoid  = depth == 0 ? pos_type{ 0, 3 } : pos_type{ 0, 0 };

	auto pos = keypad.at('A');

	size_t len = 0;
	for ( const auto key: keys ) {
		const auto end = keypad.at(key);

		const auto movements = get_movements(pos, end, avoid);

		if ( depth == max_depth ) {
			len += ranges::min_element(
			           movements, [](const auto& lhs, const auto& rhs) -> bool { return lhs.size() < rhs.size(); })
			           ->size();
		}
		else {
			auto curr_min = numeric_limits<size_t>::max();
			for ( const auto& movement: movements ) {
				curr_min = min(curr_min, find_rec(movement, depth + 1, max_depth));
			}
			len += curr_min;
		}

		pos = end;
	}

	return cache[{ string(keys), depth, max_depth }] = len;
}

size_t
find(string_view keys, const int depth)
{
	return find_rec(keys, 0, depth);
}

void
part1(const vector<string>& lines)
{
	cout << accumulate(lines.begin(), lines.end(), 0UL, [](auto init, const auto& line) {
		return init + find(line, 2) * strtoul(line.c_str(), nullptr, 10);
	}) << endl;
}

void
part2(const vector<string>& lines)
{
	cout << accumulate(lines.begin(), lines.end(), 0UL, [](auto init, const auto& line) {
		return init + find(line, 25) * strtoul(line.c_str(), nullptr, 10);
	}) << endl;
}

int
main()
{
	const auto lines = read_file("data/day21.txt");
	part1(lines);
	part2(lines);
}