From 1c2555b3574b9498347b8254ec5be90d9e0015f8 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Tue, 2 Jan 2024 20:23:16 +0100 Subject: clean up --- src/day04.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'src/day04.cpp') diff --git a/src/day04.cpp b/src/day04.cpp index 8afcb2c..0d3e552 100644 --- a/src/day04.cpp +++ b/src/day04.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -25,8 +24,8 @@ read_file(string_view filename) vector split(const string& line, char sep) { - vector parts{}; stringstream input{ line }; + vector parts; for ( string part; getline(input, part, sep); ) { parts.emplace_back(part); @@ -49,13 +48,12 @@ read_ints(const string& line) return container; } -vector -build_data_set() +auto +build_data_set(string_view filename) { - const auto lines = read_file("data/day04.txt"); - vector values{}; + vector values; - for ( const auto& line: lines ) { + for ( const auto& line: read_file(filename) ) { const auto cards = split(line, ':'); const auto numbers = split(cards[1], '|'); @@ -75,10 +73,10 @@ build_data_set() } void -pass1() +pass1(const vector& values) { auto sum = 0U; - for ( auto value: build_data_set() ) { + for ( auto value: values ) { if ( value != 0 ) { sum += (1U << (value - 1)); } @@ -88,10 +86,9 @@ pass1() } void -pass2() +pass2(const vector& values) { - const auto values = build_data_set(); - auto count = 0U; + auto count = 0U; function process = [&](size_t start, size_t end) -> void { for ( ; start != end; ++start ) { @@ -111,6 +108,7 @@ pass2() int main() { - pass1(); - pass2(); + const auto values = build_data_set("data/day04.txt"); + pass1(values); + pass2(values); } -- cgit v1.3