diff options
| -rw-r--r-- | 2024/src/day10.cpp | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/2024/src/day10.cpp b/2024/src/day10.cpp index 43dbd6e..58b797f 100644 --- a/2024/src/day10.cpp +++ b/2024/src/day10.cpp | |||
| @@ -33,10 +33,10 @@ neighbors(pos_type pos) | |||
| 33 | return { { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } }; | 33 | return { { x - 1, y }, { x, y - 1 }, { x + 1, y }, { x, y + 1 } }; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | long | 36 | size_t |
| 37 | bfs(const map<pos_type, int>& data, pos_type start_pos) | 37 | bfs(const map<pos_type, int>& data, pos_type start_pos) |
| 38 | { | 38 | { |
| 39 | long peaks = 0; | 39 | size_t peaks = 0; |
| 40 | 40 | ||
| 41 | set<pos_type> seen; | 41 | set<pos_type> seen; |
| 42 | queue<pos_type> queue; | 42 | queue<pos_type> queue; |
| @@ -77,16 +77,64 @@ part1(const map<pos_type, int>& data) | |||
| 77 | } | 77 | } |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | long sum = 0; | 80 | size_t sum = 0; |
| 81 | for ( const auto& start: starts ) { | 81 | for ( const auto& start: starts ) { |
| 82 | sum += bfs(data, start); | 82 | sum += bfs(data, start); |
| 83 | } | 83 | } |
| 84 | cout << sum << endl; | 84 | cout << sum << endl; |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | size_t | ||
| 88 | bfs2(const map<pos_type, int>& data, pos_type start_pos) | ||
| 89 | { | ||
| 90 | size_t paths = 0; | ||
| 91 | |||
| 92 | queue<pos_type> queue; | ||
| 93 | |||
| 94 | queue.push(start_pos); | ||
| 95 | |||
| 96 | while ( !queue.empty() ) { | ||
| 97 | auto pos = queue.front(); | ||
| 98 | queue.pop(); | ||
| 99 | |||
| 100 | if ( data.at(pos) == 9 ) { | ||
| 101 | ++paths; | ||
| 102 | continue; | ||
| 103 | } | ||
| 104 | |||
| 105 | for ( const auto& neighbor: neighbors(pos) ) { | ||
| 106 | if ( !data.contains(neighbor) || | ||
| 107 | data.at(neighbor) != data.at(pos) + 1 ) { | ||
| 108 | continue; | ||
| 109 | } | ||
| 110 | |||
| 111 | queue.emplace(neighbor); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | return paths; | ||
| 115 | } | ||
| 116 | |||
| 117 | void | ||
| 118 | part2(const map<pos_type, int>& data) | ||
| 119 | { | ||
| 120 | vector<pos_type> starts; | ||
| 121 | for ( const auto& [pos, value]: data ) { | ||
| 122 | if ( value == 0 ) { | ||
| 123 | starts.push_back(pos); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | size_t sum = 0; | ||
| 128 | for ( const auto& start: starts ) { | ||
| 129 | sum += bfs2(data, start); | ||
| 130 | } | ||
| 131 | cout << sum << endl; | ||
| 132 | } | ||
| 133 | |||
| 87 | int | 134 | int |
| 88 | main() | 135 | main() |
| 89 | { | 136 | { |
| 90 | auto data = read_file("data/day10.txt"); | 137 | auto data = read_file("data/day10.txt"); |
| 91 | part1(data); | 138 | part1(data); |
| 139 | part2(data); | ||
| 92 | } | 140 | } |
