aboutsummaryrefslogtreecommitdiff
path: root/src/day05.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2023-12-05 22:15:08 +0100
committerThomas Schmucker <ts@its1.de>2023-12-05 22:15:08 +0100
commitf95d6c39353700924a71014ea4b05b1d9c865284 (patch)
tree023fdd2ed7571d8a406c2cfe024c0fdf8a831426 /src/day05.cpp
parent65dfb9cf7d95ccd5a608bc85065c8af2650c1641 (diff)
downloadadvent-of-code-f95d6c39353700924a71014ea4b05b1d9c865284.tar.gz
advent-of-code-f95d6c39353700924a71014ea4b05b1d9c865284.tar.bz2
advent-of-code-f95d6c39353700924a71014ea4b05b1d9c865284.zip
es ist nicht notwendig irgendwelche Zwischenergebnisse zu speichern..
Diffstat (limited to 'src/day05.cpp')
-rw-r--r--src/day05.cpp20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/day05.cpp b/src/day05.cpp
index 26b5c66..92e39fc 100644
--- a/src/day05.cpp
+++ b/src/day05.cpp
@@ -2,6 +2,7 @@
2#include <fstream> 2#include <fstream>
3#include <iostream> 3#include <iostream>
4#include <iterator> 4#include <iterator>
5#include <limits>
5#include <map> 6#include <map>
6#include <sstream> 7#include <sstream>
7#include <string> 8#include <string>
@@ -98,7 +99,7 @@ part1()
98 range_mapping[parts[0]] = mapping; 99 range_mapping[parts[0]] = mapping;
99 } 100 }
100 101
101 vector<long> values{}; 102 auto min_value = numeric_limits<long>::max();
102 for ( auto seed: seeds ) { 103 for ( auto seed: seeds ) {
103 for ( string category{ "seed" }; !category.empty(); category = category_mapping[category] ) { 104 for ( string category{ "seed" }; !category.empty(); category = category_mapping[category] ) {
104 const auto& ranges = range_mapping[category]; 105 const auto& ranges = range_mapping[category];
@@ -110,15 +111,15 @@ part1()
110 } 111 }
111 } 112 }
112 } 113 }
113 values.emplace_back(seed); 114 min_value = min(min_value, seed);
114 } 115 }
115 cout << *min_element(values.begin(), values.end()) << endl; 116 cout << min_value << endl;
116} 117}
117 118
118void 119void
119part2() 120part2()
120{ 121{
121 auto lines = read_file("data/day05.txt"); 122 auto lines = read_file("data/day05-sample1.txt");
122 123
123 auto read_ints = [](const string& line) { 124 auto read_ints = [](const string& line) {
124 stringstream iss{ line }; 125 stringstream iss{ line };
@@ -155,10 +156,9 @@ part2()
155 range_mapping[parts[0]] = mapping; 156 range_mapping[parts[0]] = mapping;
156 } 157 }
157 158
158 vector<long> values{}; 159 // brute force -- slow, but works
160 auto min_value = numeric_limits<long>::max();
159 for ( size_t idx = 0; idx != seeds.size(); idx += 2 ) { 161 for ( size_t idx = 0; idx != seeds.size(); idx += 2 ) {
160 cout << "." << endl;
161 vector<long> min_so_far{};
162 for ( auto start = seeds[idx]; start != seeds[idx] + seeds[idx+1]; ++start ) { 162 for ( auto start = seeds[idx]; start != seeds[idx] + seeds[idx+1]; ++start ) {
163 auto seed = start; 163 auto seed = start;
164 164
@@ -172,15 +172,15 @@ part2()
172 } 172 }
173 } 173 }
174 } 174 }
175 min_so_far.emplace_back(seed); 175 min_value = min(min_value, seed);
176 } 176 }
177 values.push_back(*min_element(min_so_far.begin(), min_so_far.end()));
178 } 177 }
179 cout << *min_element(values.begin(), values.end()) << endl; 178 cout << min_value << endl;
180} 179}
181 180
182int 181int
183main() 182main()
184{ 183{
184 part1();
185 part2(); 185 part2();
186} 186}