aboutsummaryrefslogtreecommitdiff
path: root/2025/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-12-07 22:00:34 +0100
committerThomas Schmucker <ts@its1.de>2025-12-07 22:00:34 +0100
commit8c22edef785ac34236a9605d58158d12ec8dea33 (patch)
treed36c8509aedc7a0d66d9284b7064d489886af7f3 /2025/src
parent662e2317c7ff77236601c32b3b204fe89ac5cc5d (diff)
downloadadvent-of-code-8c22edef785ac34236a9605d58158d12ec8dea33.tar.gz
advent-of-code-8c22edef785ac34236a9605d58158d12ec8dea33.tar.bz2
advent-of-code-8c22edef785ac34236a9605d58158d12ec8dea33.zip
aoc 2025, day 7 (clean up)
Diffstat (limited to '2025/src')
-rw-r--r--2025/src/day07.cpp28
1 files changed, 10 insertions, 18 deletions
diff --git a/2025/src/day07.cpp b/2025/src/day07.cpp
index e902a43..6592518 100644
--- a/2025/src/day07.cpp
+++ b/2025/src/day07.cpp
@@ -14,21 +14,14 @@ namespace {
14vector<string> 14vector<string>
15read_file(const filesystem::path& filename) 15read_file(const filesystem::path& filename)
16{ 16{
17 ifstream file{ filename }; 17 ifstream file{ filename };
18 vector<string> grid; 18 return { istream_iterator<string>{ file }, {} };
19
20 for ( string line; getline(file, line); ) {
21 grid.emplace_back(line);
22 }
23
24 return grid;
25} 19}
26 20
27void 21void
28part1(const vector<string>& grid) 22part1(const vector<string>& grid)
29{ 23{
30 set<size_t> curr; 24 set<size_t> curr{ grid[0].find('S') };
31 curr.insert(grid[0].find('S'));
32 25
33 int count = 0; 26 int count = 0;
34 for ( size_t i = 1; i < grid.size(); ++i ) { 27 for ( size_t i = 1; i < grid.size(); ++i ) {
@@ -36,11 +29,10 @@ part1(const vector<string>& grid)
36 29
37 auto next = curr; 30 auto next = curr;
38 31
39 for ( const auto pos: curr ) { 32 for ( const auto col: curr ) {
40 if ( row[pos] == '^' ) { 33 if ( row[col] == '^' ) {
41 next.erase(pos); 34 next.erase(col);
42 next.insert(pos - 1); 35 next.insert({ col - 1, col + 1 });
43 next.insert(pos + 1);
44 ++count; 36 ++count;
45 } 37 }
46 } 38 }
@@ -55,7 +47,8 @@ part2(const vector<string>& grid)
55{ 47{
56 using Pos = tuple<size_t, size_t>; 48 using Pos = tuple<size_t, size_t>;
57 49
58 map<Pos, long> cache; 50 map<Pos, long> cache;
51
59 function<long(const Pos&)> walk = [&](const Pos& pos) { 52 function<long(const Pos&)> walk = [&](const Pos& pos) {
60 if ( cache.contains(pos) ) { 53 if ( cache.contains(pos) ) {
61 return cache.at(pos); 54 return cache.at(pos);
@@ -77,8 +70,7 @@ part2(const vector<string>& grid)
77 return (cache[pos] = walk({ col, row + 1 })); 70 return (cache[pos] = walk({ col, row + 1 }));
78 }; 71 };
79 72
80 const auto col = grid[0].find('S'); 73 cout << "Part 2: " << walk(Pos{ grid[0].find('S'), 0 }) << '\n';
81 cout << "Part 2: " << walk(Pos{ col, 0 }) << '\n';
82} 74}
83 75
84} // namespace 76} // namespace