aboutsummaryrefslogtreecommitdiff
path: root/2024/src/day20.cpp
blob: 091c2ab6acc10d0704c383070ae3e5db8fcc6cb4 (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
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using pos_type = tuple<size_t, size_t>;

tuple<pos_type, pos_type, set<pos_type>>
read_file(string_view filename)
{
	fstream       input{ filename };
	set<pos_type> walls;
	pos_type      start;
	pos_type      end;

	size_t yPos = 0;
	for ( string line; getline(input, line); ) {
		for ( size_t xPos = 0; xPos != line.size(); ++xPos ) {
			auto chr = line[xPos];
			if ( chr == '#' ) {
				walls.emplace(xPos, yPos);
			}
			else if ( chr == 'S' ) {
				start = { xPos, yPos };
			}
			else if ( chr == 'E' ) {
				end = { xPos, yPos };
			}
		}
		++yPos;
	}

	return { start, end, walls };
}

map<pos_type, long>
bfs(const tuple<pos_type, pos_type, set<pos_type>>& data)
{
	const auto& [start, end, walls] = data;

	map<pos_type, long> path;

	queue<tuple<pos_type, long>> queue;
	queue.push({ start, 0 });

	while ( !queue.empty() ) {
		auto [pos, dist] = queue.front();
		queue.pop();

		if ( path.contains(pos) ) {
			continue;
		}
		path[pos] = dist;

		if ( pos == end ) {
			break;
		}

		auto [x, y] = pos;
		for ( const auto& new_pos: vector<pos_type>{ { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } } ) {
			if ( !walls.contains(new_pos) ) {
				queue.push({ new_pos, dist + 1 });
			}
		}
	}
	return path;
}

void
part1(const tuple<pos_type, pos_type, set<pos_type>>& data)
{
	auto path = bfs(data);

	const auto& [start, end, walls] = data;

	long count = 0;
	for ( const auto& [pos, dist]: path ) {
		auto [x, y] = pos;

		for ( const auto& [dx, dy]: vector<pos_type>{ { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } } ) {
			if ( !walls.contains({ x + dx, y + dy }) ) {
				continue;
			}
			pos_type new_pos{ x + 2 * dx, y + 2 * dy };
			if ( path.contains(new_pos) && path.at(new_pos) - dist >= 100 + 2 ) {
				++count;
			}
		}
	}
	cout << count << endl;
}

void
part2(const tuple<pos_type, pos_type, set<pos_type>>& data)
{
	auto path = bfs(data);

	long count = 0;
	for ( const auto& [pos, dist]: path ) {
		auto [x, y] = pos;

		for ( size_t r = 2; r != 21; ++r ) {
			set<pos_type> candidates;
			for ( size_t dx = 0; dx != r + 1; ++dx ) {
				size_t dy = r - dx;

				candidates.insert({ x + dx, y + dy });
				candidates.insert({ x - dx, y + dy });
				candidates.insert({ x + dx, y - dy });
				candidates.insert({ x - dx, y - dy });
			}

			auto temp_dist = dist;
			count += ranges::count_if(candidates, [&](const auto& new_pos) {
				return path.contains(new_pos) && path.at(new_pos) - temp_dist >= 100 + long(r);
			});
		}
	}
	cout << count << endl;
}

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