diff options
| author | Thomas Schmucker <ts@its1.de> | 2023-12-27 14:31:23 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2023-12-27 14:31:23 +0100 |
| commit | 4317917a8500f29a5af588d25912404c300fcedb (patch) | |
| tree | 2957fd041ac96c8fb8b31ec098cd49eeae5fadb8 /src | |
| parent | 6b289756222dfd6c112b2b5a67689819e5b2ae4c (diff) | |
| download | advent-of-code-4317917a8500f29a5af588d25912404c300fcedb.tar.gz advent-of-code-4317917a8500f29a5af588d25912404c300fcedb.tar.bz2 advent-of-code-4317917a8500f29a5af588d25912404c300fcedb.zip | |
Lösungen für Tag 23, Teil 1
Diffstat (limited to 'src')
| -rw-r--r-- | src/day23.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/day23.cpp b/src/day23.cpp new file mode 100644 index 0000000..6e60e55 --- /dev/null +++ b/src/day23.cpp | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <array> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <iterator> | ||
| 6 | #include <queue> | ||
| 7 | #include <set> | ||
| 8 | #include <string> | ||
| 9 | #include <vector> | ||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | vector<string> | ||
| 13 | read_file(string_view filename) | ||
| 14 | { | ||
| 15 | fstream input{ filename }; | ||
| 16 | vector<string> data; | ||
| 17 | |||
| 18 | for ( string line; getline(input, line); ) { | ||
| 19 | data.emplace_back(line); | ||
| 20 | } | ||
| 21 | |||
| 22 | return data; | ||
| 23 | } | ||
| 24 | |||
| 25 | set<tuple<size_t, size_t>> | ||
| 26 | get_neighbours(const vector<string>& maze, tuple<size_t, size_t> possition) | ||
| 27 | { | ||
| 28 | static const tuple<size_t, size_t> deltas[] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; // NOLINT | ||
| 29 | |||
| 30 | static const auto SYM_UP = '^'; | ||
| 31 | static const auto SYM_RIGHT = '>'; | ||
| 32 | static const auto SYM_DOWN = 'v'; | ||
| 33 | static const auto SYM_LEFT = '<'; | ||
| 34 | |||
| 35 | const auto [row, col] = possition; | ||
| 36 | |||
| 37 | switch ( maze[row][col] ) { | ||
| 38 | case SYM_UP: | ||
| 39 | return { make_tuple(row - 1, col) }; | ||
| 40 | case SYM_RIGHT: | ||
| 41 | return { make_tuple(row, col + 1) }; | ||
| 42 | case SYM_DOWN: | ||
| 43 | return { make_tuple(row + 1, col) }; | ||
| 44 | case SYM_LEFT: | ||
| 45 | return { make_tuple(row, col - 1) }; | ||
| 46 | } | ||
| 47 | |||
| 48 | set<tuple<size_t, size_t>> positions; | ||
| 49 | |||
| 50 | for ( const auto& delta: deltas ) { | ||
| 51 | const auto new_row = row + get<0>(delta); | ||
| 52 | const auto new_col = col + get<1>(delta); | ||
| 53 | |||
| 54 | if ( new_row >= maze.size() || new_col >= maze[0].size() ) { | ||
| 55 | continue; | ||
| 56 | } | ||
| 57 | |||
| 58 | if ( maze[new_row][new_col] == '#' ) { | ||
| 59 | continue; | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( new_row < row && maze[new_row][new_col] == SYM_DOWN ) { | ||
| 63 | continue; | ||
| 64 | } | ||
| 65 | if ( new_row > row && maze[new_row][new_col] == SYM_UP ) { | ||
| 66 | continue; | ||
| 67 | } | ||
| 68 | if ( new_col < col && maze[new_row][new_col] == SYM_RIGHT ) { | ||
| 69 | continue; | ||
| 70 | } | ||
| 71 | if ( new_col > col && maze[new_row][new_col] == SYM_LEFT ) { | ||
| 72 | continue; | ||
| 73 | } | ||
| 74 | |||
| 75 | positions.emplace(new_row, new_col); | ||
| 76 | } | ||
| 77 | |||
| 78 | return positions; | ||
| 79 | } | ||
| 80 | |||
| 81 | void | ||
| 82 | part1(const vector<string>& maze) | ||
| 83 | { | ||
| 84 | const tuple<size_t, size_t> start = { 0, maze[0].find('.') }; | ||
| 85 | const tuple<size_t, size_t> end = { maze.size() - 1, maze[maze.size() - 1].find('.') }; | ||
| 86 | |||
| 87 | queue<tuple<tuple<size_t, size_t>, set<tuple<size_t, size_t>>>> queue; | ||
| 88 | |||
| 89 | queue.emplace(start, set<tuple<size_t, size_t>>{}); | ||
| 90 | |||
| 91 | size_t longest = 0; | ||
| 92 | while ( !queue.empty() ) { | ||
| 93 | auto [position, seen] = queue.front(); | ||
| 94 | queue.pop(); | ||
| 95 | |||
| 96 | while ( position != end ) { | ||
| 97 | seen.emplace(position); | ||
| 98 | |||
| 99 | const auto neighbours = get_neighbours(maze, position); | ||
| 100 | |||
| 101 | vector<tuple<size_t, size_t>> next_positions; | ||
| 102 | |||
| 103 | // { neighbours \ seen } | ||
| 104 | set_difference(neighbours.begin(), neighbours.end(), | ||
| 105 | seen.begin(), seen.end(), | ||
| 106 | back_inserter(next_positions)); | ||
| 107 | |||
| 108 | if ( next_positions.empty() ) { | ||
| 109 | break; | ||
| 110 | } | ||
| 111 | |||
| 112 | position = next_positions[0]; | ||
| 113 | |||
| 114 | for ( size_t idx = 1; idx != next_positions.size(); ++idx ) { | ||
| 115 | queue.emplace(next_positions[idx], seen); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | if ( position == end ) { | ||
| 119 | longest = max(longest, seen.size()); | ||
| 120 | } | ||
| 121 | } | ||
| 122 | cout << longest << endl; | ||
| 123 | } | ||
| 124 | |||
| 125 | int | ||
| 126 | main() | ||
| 127 | { | ||
| 128 | const auto maze = read_file("data/day23.txt"); | ||
| 129 | part1(maze); | ||
| 130 | } | ||
