aboutsummaryrefslogtreecommitdiff
path: root/src/day23.cpp
blob: d8c20d3d5c3f5f2936c810f20726878684a34c81 (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
#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

using position_t = tuple<size_t, size_t>;

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

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

	return data;
}

set<position_t>
get_neighbours(const vector<string>& maze, position_t possition)
{
	static const position_t deltas[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; // NOLINT

	static const auto SYM_UP    = '^';
	static const auto SYM_RIGHT = '>';
	static const auto SYM_DOWN  = 'v';
	static const auto SYM_LEFT  = '<';

	const auto [row, col] = possition;

	switch ( maze[row][col] ) {
	case SYM_UP:
		return { make_tuple(row - 1, col) };
	case SYM_RIGHT:
		return { make_tuple(row, col + 1) };
	case SYM_DOWN:
		return { make_tuple(row + 1, col) };
	case SYM_LEFT:
		return { make_tuple(row, col - 1) };
	}

	set<position_t> positions;

	for ( const auto& delta: deltas ) {
		const auto new_row = row + get<0>(delta);
		const auto new_col = col + get<1>(delta);

		if ( new_row >= maze.size() || new_col >= maze[0].size() ) {
			continue;
		}

		if ( maze[new_row][new_col] == '#' ) {
			continue;
		}

		if ( new_row < row && maze[new_row][new_col] == SYM_DOWN ) {
			continue;
		}
		if ( new_row > row && maze[new_row][new_col] == SYM_UP ) {
			continue;
		}
		if ( new_col < col && maze[new_row][new_col] == SYM_RIGHT ) {
			continue;
		}
		if ( new_col > col && maze[new_row][new_col] == SYM_LEFT ) {
			continue;
		}

		positions.emplace(new_row, new_col);
	}

	return positions;
}

set<position_t>
get_neighbours2(const vector<string>& maze, position_t possition)
{
	static const position_t deltas[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; // NOLINT

	const auto [row, col] = possition;

	set<position_t> positions;

	for ( const auto& delta: deltas ) {
		const auto new_row = row + get<0>(delta);
		const auto new_col = col + get<1>(delta);

		if ( new_row >= maze.size() || new_col >= maze[0].size() ) {
			continue;
		}

		if ( maze[new_row][new_col] == '#' ) {
			continue;
		}

		positions.emplace(new_row, new_col);
	}

	return positions;
}

void
solve(const vector<string>& maze, function<set<position_t>(const vector<string>&, position_t)> neighbours)
{
	const position_t start = { 0, maze[0].find('.') };
	const position_t end   = { maze.size() - 1, maze[maze.size() - 1].find('.') };

	vector<vector<bool>> visited(maze.size(), vector<bool>(maze[0].size()));

	function<long(position_t, long)> find_longest_path = [&](position_t position, long current) -> long {
		const auto [row, col] = position;

		if ( visited[row][col] ) {
			return 0;
		}

		if ( position == end ) {
			return current;
		}

		long value = 0;

		visited[row][col] = true;
		for ( const auto& neighbour: neighbours(maze, position) ) {
			value = max(value, find_longest_path(neighbour, current + 1));
		}
		visited[row][col] = false;

		return value;
	};

	auto value = find_longest_path(start, 0);
	cout << value << endl;
}

int
main()
{
	const auto maze = read_file("data/day23.txt");
	solve(maze, get_neighbours);
	solve(maze, get_neighbours2);
}