Skip to content

Commit 186754c

Browse files
committed
aoc_lib: add helper function to read lines into a vector
1 parent 30161e5 commit 186754c

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

2023/src/day23.hpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class TrailMap {
6363

6464
public:
6565
explicit TrailMap(const std::vector<std::string> &grid_);
66-
static TrailMap read(std::istream &);
66+
static TrailMap read(std::istream &is) {
67+
return TrailMap{aoc::read_lines(is)};
68+
}
6769
int part_1() const;
6870
int part_2() const;
6971
};
@@ -82,15 +84,6 @@ TrailMap::Key TrailMap::pos_to_key(const Pos &pos) {
8284

8385
Pos TrailMap::key_to_pos(const Key key) const { return key_positions[key]; }
8486

85-
TrailMap TrailMap::read(std::istream &is) {
86-
std::vector<std::string> grid;
87-
std::string line;
88-
while (std::getline(is, line)) {
89-
grid.push_back(std::move(line));
90-
}
91-
return TrailMap{grid};
92-
}
93-
9487
TrailMap::TrailMap(const std::vector<std::string> &grid_) : grid(grid_) {
9588
assert(grid.height > 2 && grid.width > 2);
9689
// starting point

2024/src/day04.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ bool check_mas(const Grid<char> &grid, const Pos &p) {
7878
}
7979

8080
Grid<char> read_input(std::istream &is) {
81-
std::vector<std::string> lines;
82-
std::string line;
83-
while (std::getline(is, line)) {
84-
lines.push_back(std::move(line));
85-
}
86-
return Grid<char>{std::move(lines)};
81+
return Grid<char>{aoc::read_lines(is)};
8782
}
8883

8984
} // namespace aoc::day04

2024/src/day12.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ class Garden {
6565
};
6666

6767
Garden Garden::read(std::istream &is) {
68-
// read file line-by-line
69-
std::vector<std::string> plot_data;
70-
std::string line;
71-
while (std::getline(is, line)) {
72-
plot_data.push_back(line);
73-
}
74-
Garden garden{aoc::ds::Grid<char>{plot_data}};
68+
Garden garden{aoc::ds::Grid<char>{aoc::read_lines(is)}};
7569
garden.plots.for_each_with_pos(
7670
std::bind_front(&Garden::process_plot, &garden));
7771
return garden;

aoc_lib/src/lib.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <string> // for string, operator==
2424
#include <type_traits> // for underlying_type_t, is_same_v, is_signed_v, conditional_t, is_const_v
2525
#include <utility> // for move
26+
#include <vector> // for vector
2627

2728
namespace aoc {
2829

@@ -489,6 +490,18 @@ std::string read_whole_stream(std::istream &is) {
489490
return std::string{std::istreambuf_iterator<char>(is), {}};
490491
}
491492

493+
/**
494+
* @brief Reads lines from a stream.
495+
*/
496+
std::vector<std::string> read_lines(std::istream &is) {
497+
std::vector<std::string> lines;
498+
std::string line;
499+
while (std::getline(is, line)) {
500+
lines.push_back(std::move(line));
501+
}
502+
return lines;
503+
}
504+
492505
} // namespace aoc
493506

494507
template <std::integral int_type>

0 commit comments

Comments
 (0)