aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-12-10 18:29:48 +0100
committerThomas Schmucker <ts@its1.de>2024-12-10 18:29:48 +0100
commit118f8d9868c74d27a8a66573e7fbceab835f8bba (patch)
tree26bc838f6a10acd59198b03f18041997a4397d1b /2024
parentdf47c6d9dae41768a0b4f25a0e16a296ce3bbef7 (diff)
downloadadvent-of-code-118f8d9868c74d27a8a66573e7fbceab835f8bba.tar.gz
advent-of-code-118f8d9868c74d27a8a66573e7fbceab835f8bba.tar.bz2
advent-of-code-118f8d9868c74d27a8a66573e7fbceab835f8bba.zip
aoc 2024, day 10, part 2
Diffstat (limited to '2024')
-rw-r--r--2024/src/day10.cpp54
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
36long 36size_t
37bfs(const map<pos_type, int>& data, pos_type start_pos) 37bfs(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
87size_t
88bfs2(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
117void
118part2(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
87int 134int
88main() 135main()
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}