File tree Expand file tree Collapse file tree 7 files changed +25
-44
lines changed Expand file tree Collapse file tree 7 files changed +25
-44
lines changed Original file line number Diff line number Diff line change 88#ifndef DAY07_HPP_QKDJV1MU
99#define DAY07_HPP_QKDJV1MU
1010
11+ #include " lib.hpp" // for read_vector
1112#include < algorithm> // for sort
1213#include < array> // for array
1314#include < cassert> // for assert
@@ -180,6 +181,7 @@ struct Hand {
180181 int bid;
181182 HandType hand_type;
182183
184+ Hand () : Hand({}, 0 ) {}
183185 Hand (const std::array<Card, 5 > &cards, int bid)
184186 : cards(cards), bid(bid), hand_type(identify_hand(cards)){};
185187 Hand (const Card &card1, const Card &card2, const Card &card3,
@@ -226,12 +228,7 @@ std::ostream &operator<<(std::ostream &os, const Hand &hand) {
226228}
227229
228230std::vector<Hand> read_hands (std::istream &is) {
229- std::vector<Hand> hands;
230- Hand hand{{}, 0 };
231- while (is >> hand) {
232- hands.push_back (std::move (hand));
233- }
234- return hands;
231+ return aoc::read_vector<Hand>(is);
235232}
236233
237234} // namespace aoc::day07
Original file line number Diff line number Diff line change 88#ifndef DAY09_HPP_UZDLAO3M
99#define DAY09_HPP_UZDLAO3M
1010
11+ #include " lib.hpp" // for read_vector
1112#include < cassert> // for assert
1213#include < cstddef> // for size_t
1314#include < sstream> // for istringstream
@@ -18,12 +19,7 @@ namespace aoc::day09 {
1819
1920std::vector<int > read_history (const std::string &line) {
2021 std::istringstream ss{line};
21- std::vector<int > history;
22- int x;
23- while (ss >> x) {
24- history.push_back (x);
25- }
26- return history;
22+ return aoc::read_vector<int >(ss);
2723}
2824
2925std::vector<int > diff (const std::vector<int > &vec) {
Original file line number Diff line number Diff line change 99#define DAY24_HPP_PKLXENFE
1010
1111#include " ds/grid.hpp" // for Grid
12- #include " lib.hpp" // for expect_input
12+ #include " lib.hpp" // for expect_input, read_vector
1313#include < cassert> // for assert
1414#include < cmath> // for copysign
1515#include < cstddef> // for size_t
@@ -137,12 +137,7 @@ make_system(const std::vector<aoc::day24::Hailstone> &stones,
137137}
138138
139139std::vector<Hailstone> read_stones (std::istream &is) {
140- std::vector<Hailstone> stones;
141- Hailstone stone{};
142- while (is >> stone) {
143- stones.push_back (std::move (stone));
144- }
145- return stones;
140+ return aoc::read_vector<Hailstone>(is);
146141}
147142
148143} // namespace aoc::day24
Original file line number Diff line number Diff line change 88#ifndef DAY02_HPP_QMMUZCBK
99#define DAY02_HPP_QMMUZCBK
1010
11+ #include " lib.hpp" // for read_vector
1112#include < algorithm> // for copy
1213#include < cstdlib> // for abs, size_t
1314#include < iostream> // for istream
1415#include < sstream> // for istringstream
1516#include < string> // for string, getline
16- #include < utility> // for move
1717#include < vector> // for vector
1818
1919namespace aoc ::day02 {
@@ -57,12 +57,7 @@ auto read_input(std::istream &is) {
5757 std::vector<std::vector<int >> reports;
5858 while (std::getline (is, line)) {
5959 std::istringstream ss (line);
60- std::vector<int > levels;
61- int tmp;
62- while (ss >> tmp) {
63- levels.push_back (tmp);
64- }
65- reports.push_back (std::move (levels));
60+ reports.push_back (aoc::read_vector<int >(ss));
6661 }
6762 return reports;
6863}
Original file line number Diff line number Diff line change @@ -67,10 +67,7 @@ std::istream &operator>>(std::istream &is, Equation &eqn) {
6767 if (std::getline (is, line)) {
6868 std::istringstream iss{line};
6969 iss >> test_value >> expect_input (' :' );
70- long x;
71- while (iss >> x) {
72- operands.push_back (x);
73- }
70+ operands = aoc::read_vector<long >(iss);
7471 }
7572 if (is) {
7673 eqn.test_value = test_value;
@@ -79,14 +76,7 @@ std::istream &operator>>(std::istream &is, Equation &eqn) {
7976 return is;
8077}
8178
82- auto read_input (std::istream &is) {
83- std::vector<Equation> equations;
84- Equation eqn;
85- while (is >> eqn) {
86- equations.push_back (std::move (eqn));
87- }
88- return equations;
89- }
79+ auto read_input (std::istream &is) { return aoc::read_vector<Equation>(is); }
9080
9181} // namespace aoc::day07
9282
Original file line number Diff line number Diff line change @@ -389,12 +389,7 @@ std::vector<Code> read_input(std::istream &is) {
389389 std::string line;
390390 while (std::getline (is, line)) {
391391 std::istringstream iss{line};
392- Code code{std::move (line)};
393- Key key{};
394- while (iss >> key) {
395- code.keys .push_back (key);
396- }
397- codes.push_back (std::move (code));
392+ codes.push_back ({std::move (line), aoc::read_vector<Key>(iss)});
398393 }
399394 return codes;
400395}
Original file line number Diff line number Diff line change @@ -504,6 +504,19 @@ std::vector<std::string> read_lines(std::istream &is) {
504504 return lines;
505505}
506506
507+ /* *
508+ * @brief Reads arbitrary objects from a stream into a vector.
509+ */
510+ template <typename T>
511+ std::vector<T> read_vector (std::istream &is) {
512+ std::vector<T> vec;
513+ T t{};
514+ while (is >> t) {
515+ vec.push_back (std::move (t));
516+ }
517+ return vec;
518+ }
519+
507520} // namespace aoc
508521
509522template <std::integral int_type>
You can’t perform that action at this time.
0 commit comments