diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-12-20 17:26:52 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-12-20 17:26:52 +0100 |
| commit | 7781a6d76db0a87aecb023b138d77139e44cfc27 (patch) | |
| tree | 14a3333a90ca504e72574ee76fa5820b4769bcd8 /2024/src/day20.cpp | |
| parent | 1a4db2303352c067d22fa9aac84921759e003192 (diff) | |
| download | advent-of-code-7781a6d76db0a87aecb023b138d77139e44cfc27.tar.gz advent-of-code-7781a6d76db0a87aecb023b138d77139e44cfc27.tar.bz2 advent-of-code-7781a6d76db0a87aecb023b138d77139e44cfc27.zip | |
aoc 2024, day 20, cleanup
Diffstat (limited to '2024/src/day20.cpp')
| -rw-r--r-- | 2024/src/day20.cpp | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/2024/src/day20.cpp b/2024/src/day20.cpp index 3e79f33..091c2ab 100644 --- a/2024/src/day20.cpp +++ b/2024/src/day20.cpp | |||
| @@ -45,8 +45,6 @@ bfs(const tuple<pos_type, pos_type, set<pos_type>>& data) | |||
| 45 | 45 | ||
| 46 | map<pos_type, long> path; | 46 | map<pos_type, long> path; |
| 47 | 47 | ||
| 48 | set<pos_type> seen; | ||
| 49 | |||
| 50 | queue<tuple<pos_type, long>> queue; | 48 | queue<tuple<pos_type, long>> queue; |
| 51 | queue.push({ start, 0 }); | 49 | queue.push({ start, 0 }); |
| 52 | 50 | ||
| @@ -54,11 +52,9 @@ bfs(const tuple<pos_type, pos_type, set<pos_type>>& data) | |||
| 54 | auto [pos, dist] = queue.front(); | 52 | auto [pos, dist] = queue.front(); |
| 55 | queue.pop(); | 53 | queue.pop(); |
| 56 | 54 | ||
| 57 | if ( seen.contains(pos) ) { | 55 | if ( path.contains(pos) ) { |
| 58 | continue; | 56 | continue; |
| 59 | } | 57 | } |
| 60 | seen.insert(pos); | ||
| 61 | |||
| 62 | path[pos] = dist; | 58 | path[pos] = dist; |
| 63 | 59 | ||
| 64 | if ( pos == end ) { | 60 | if ( pos == end ) { |
| @@ -67,10 +63,9 @@ bfs(const tuple<pos_type, pos_type, set<pos_type>>& data) | |||
| 67 | 63 | ||
| 68 | auto [x, y] = pos; | 64 | auto [x, y] = pos; |
| 69 | for ( const auto& new_pos: vector<pos_type>{ { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } } ) { | 65 | for ( const auto& new_pos: vector<pos_type>{ { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } } ) { |
| 70 | if ( walls.contains(new_pos) ) { | 66 | if ( !walls.contains(new_pos) ) { |
| 71 | continue; | 67 | queue.push({ new_pos, dist + 1 }); |
| 72 | } | 68 | } |
| 73 | queue.push({ new_pos, dist + 1 }); | ||
| 74 | } | 69 | } |
| 75 | } | 70 | } |
| 76 | return path; | 71 | return path; |
| @@ -92,7 +87,7 @@ part1(const tuple<pos_type, pos_type, set<pos_type>>& data) | |||
| 92 | continue; | 87 | continue; |
| 93 | } | 88 | } |
| 94 | pos_type new_pos{ x + 2 * dx, y + 2 * dy }; | 89 | pos_type new_pos{ x + 2 * dx, y + 2 * dy }; |
| 95 | if ( path.contains(new_pos) && dist < path.at(new_pos) && path.at(new_pos) - dist >= 100 + 2 ) { | 90 | if ( path.contains(new_pos) && path.at(new_pos) - dist >= 100 + 2 ) { |
| 96 | ++count; | 91 | ++count; |
| 97 | } | 92 | } |
| 98 | } | 93 | } |
| @@ -110,15 +105,20 @@ part2(const tuple<pos_type, pos_type, set<pos_type>>& data) | |||
| 110 | auto [x, y] = pos; | 105 | auto [x, y] = pos; |
| 111 | 106 | ||
| 112 | for ( size_t r = 2; r != 21; ++r ) { | 107 | for ( size_t r = 2; r != 21; ++r ) { |
| 108 | set<pos_type> candidates; | ||
| 113 | for ( size_t dx = 0; dx != r + 1; ++dx ) { | 109 | for ( size_t dx = 0; dx != r + 1; ++dx ) { |
| 114 | size_t dy = r - dx; | 110 | size_t dy = r - dx; |
| 115 | 111 | ||
| 116 | for ( const auto& new_pos: set<pos_type>{ { x + dx, y + dy }, { x - dx, y + dy }, { x + dx, y - dy }, { x - dx, y - dy } } ) { | 112 | candidates.insert({ x + dx, y + dy }); |
| 117 | if ( path.contains(new_pos) && dist < path.at(new_pos) && path.at(new_pos) - dist >= 100 + long(r) ) { | 113 | candidates.insert({ x - dx, y + dy }); |
| 118 | ++count; | 114 | candidates.insert({ x + dx, y - dy }); |
| 119 | } | 115 | candidates.insert({ x - dx, y - dy }); |
| 120 | } | ||
| 121 | } | 116 | } |
| 117 | |||
| 118 | auto temp_dist = dist; | ||
| 119 | count += ranges::count_if(candidates, [&](const auto& new_pos) { | ||
| 120 | return path.contains(new_pos) && path.at(new_pos) - temp_dist >= 100 + long(r); | ||
| 121 | }); | ||
| 122 | } | 122 | } |
| 123 | } | 123 | } |
| 124 | cout << count << endl; | 124 | cout << count << endl; |
