aboutsummaryrefslogtreecommitdiff
path: root/src/day04.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-01-02 20:23:16 +0100
committerThomas Schmucker <ts@its1.de>2024-01-02 20:23:16 +0100
commit1c2555b3574b9498347b8254ec5be90d9e0015f8 (patch)
treef43a5e8dc523af49cdd5a2c7389a3977b0da9c88 /src/day04.cpp
parente95d22d3927549caa5c259a59aedaa67d3b636ca (diff)
downloadadvent-of-code-1c2555b3574b9498347b8254ec5be90d9e0015f8.tar.gz
advent-of-code-1c2555b3574b9498347b8254ec5be90d9e0015f8.tar.bz2
advent-of-code-1c2555b3574b9498347b8254ec5be90d9e0015f8.zip
clean up
Diffstat (limited to 'src/day04.cpp')
-rw-r--r--src/day04.cpp26
1 files changed, 12 insertions, 14 deletions
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 @@
1#include <cctype>
2#include <fstream> 1#include <fstream>
3#include <iostream> 2#include <iostream>
4#include <set> 3#include <set>
@@ -25,8 +24,8 @@ read_file(string_view filename)
25vector<string> 24vector<string>
26split(const string& line, char sep) 25split(const string& line, char sep)
27{ 26{
28 vector<string> parts{};
29 stringstream input{ line }; 27 stringstream input{ line };
28 vector<string> parts;
30 29
31 for ( string part; getline(input, part, sep); ) { 30 for ( string part; getline(input, part, sep); ) {
32 parts.emplace_back(part); 31 parts.emplace_back(part);
@@ -49,13 +48,12 @@ read_ints(const string& line)
49 return container; 48 return container;
50} 49}
51 50
52vector<unsigned long> 51auto
53build_data_set() 52build_data_set(string_view filename)
54{ 53{
55 const auto lines = read_file("data/day04.txt"); 54 vector<unsigned long> values;
56 vector<unsigned long> values{};
57 55
58 for ( const auto& line: lines ) { 56 for ( const auto& line: read_file(filename) ) {
59 const auto cards = split(line, ':'); 57 const auto cards = split(line, ':');
60 const auto numbers = split(cards[1], '|'); 58 const auto numbers = split(cards[1], '|');
61 59
@@ -75,10 +73,10 @@ build_data_set()
75} 73}
76 74
77void 75void
78pass1() 76pass1(const vector<unsigned long>& values)
79{ 77{
80 auto sum = 0U; 78 auto sum = 0U;
81 for ( auto value: build_data_set() ) { 79 for ( auto value: values ) {
82 if ( value != 0 ) { 80 if ( value != 0 ) {
83 sum += (1U << (value - 1)); 81 sum += (1U << (value - 1));
84 } 82 }
@@ -88,10 +86,9 @@ pass1()
88} 86}
89 87
90void 88void
91pass2() 89pass2(const vector<unsigned long>& values)
92{ 90{
93 const auto values = build_data_set(); 91 auto count = 0U;
94 auto count = 0U;
95 92
96 function<void(size_t, size_t)> process = [&](size_t start, size_t end) -> void { 93 function<void(size_t, size_t)> process = [&](size_t start, size_t end) -> void {
97 for ( ; start != end; ++start ) { 94 for ( ; start != end; ++start ) {
@@ -111,6 +108,7 @@ pass2()
111int 108int
112main() 109main()
113{ 110{
114 pass1(); 111 const auto values = build_data_set("data/day04.txt");
115 pass2(); 112 pass1(values);
113 pass2(values);
116} 114}