aboutsummaryrefslogtreecommitdiff
path: root/2025/src/day03.cpp
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-12-03 17:32:56 +0100
committerThomas Schmucker <ts@its1.de>2025-12-03 17:32:56 +0100
commit07af163dd48bf0a496694e71aebfcb86b9c7600e (patch)
tree4589a9a93199743976bc45c792b408521fc42485 /2025/src/day03.cpp
parent765223e3a1bd4d3be8cb2c8b11e2b836e5690038 (diff)
downloadadvent-of-code-07af163dd48bf0a496694e71aebfcb86b9c7600e.tar.gz
advent-of-code-07af163dd48bf0a496694e71aebfcb86b9c7600e.tar.bz2
advent-of-code-07af163dd48bf0a496694e71aebfcb86b9c7600e.zip
aoc 2025, day 3, part 2
Diffstat (limited to '2025/src/day03.cpp')
-rw-r--r--2025/src/day03.cpp39
1 files changed, 32 insertions, 7 deletions
diff --git a/2025/src/day03.cpp b/2025/src/day03.cpp
index c131255..ebf5fd4 100644
--- a/2025/src/day03.cpp
+++ b/2025/src/day03.cpp
@@ -3,6 +3,7 @@
3#include <filesystem> 3#include <filesystem>
4#include <fstream> 4#include <fstream>
5#include <iostream> 5#include <iostream>
6#include <numeric>
6#include <vector> 7#include <vector>
7 8
8using namespace std; 9using namespace std;
@@ -28,16 +29,39 @@ read_file(const filesystem::path& filename)
28 return lines; 29 return lines;
29} 30}
30 31
32long
33solve(const vector<long>& line, int n)
34{
35 long value = 0;
36 auto start = line.begin();
37 auto end = prev(line.end(), n);
38
39 while ( n-- > 0 ) {
40 start = max_element(start, end++);
41
42 value *= 10;
43 value += *start++;
44 }
45 return value;
46}
47
48long
49solve(const vector<vector<long>>& lines, int n)
50{
51 return accumulate(lines.begin(), lines.end(), 0L,
52 [&](auto sum, const auto& line) { return sum + solve(line, n); });
53}
54
31void 55void
32part1(const vector<vector<long>>& lines) 56part1(const vector<vector<long>>& lines)
33{ 57{
34 long sum = 0; 58 cout << "Part 1: " << solve(lines, 2) << '\n';
35 for ( const auto& line: lines ) { 59}
36 auto first = max_element(line.begin(), prev(line.end())); 60
37 auto second = max_element(next(first), line.end()); 61void
38 sum += *first * 10 + *second; 62part2(const vector<vector<long>>& lines)
39 } 63{
40 cout << "Part 1: " << sum << '\n'; 64 cout << "Part 2: " << solve(lines, 12) << '\n';
41} 65}
42 66
43} // namespace 67} // namespace
@@ -47,4 +71,5 @@ main()
47{ 71{
48 auto lines = read_file("data/day03.txt"); 72 auto lines = read_file("data/day03.txt");
49 part1(lines); 73 part1(lines);
74 part2(lines);
50} 75}