From 8c22edef785ac34236a9605d58158d12ec8dea33 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 7 Dec 2025 22:00:34 +0100 Subject: aoc 2025, day 7 (clean up) --- 2025/src/day07.cpp | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) (limited to '2025/src/day07.cpp') 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 { vector read_file(const filesystem::path& filename) { - ifstream file{ filename }; - vector grid; - - for ( string line; getline(file, line); ) { - grid.emplace_back(line); - } - - return grid; + ifstream file{ filename }; + return { istream_iterator{ file }, {} }; } void part1(const vector& grid) { - set curr; - curr.insert(grid[0].find('S')); + set curr{ grid[0].find('S') }; int count = 0; for ( size_t i = 1; i < grid.size(); ++i ) { @@ -36,11 +29,10 @@ part1(const vector& grid) auto next = curr; - for ( const auto pos: curr ) { - if ( row[pos] == '^' ) { - next.erase(pos); - next.insert(pos - 1); - next.insert(pos + 1); + for ( const auto col: curr ) { + if ( row[col] == '^' ) { + next.erase(col); + next.insert({ col - 1, col + 1 }); ++count; } } @@ -55,7 +47,8 @@ part2(const vector& grid) { using Pos = tuple; - map cache; + map cache; + function walk = [&](const Pos& pos) { if ( cache.contains(pos) ) { return cache.at(pos); @@ -77,8 +70,7 @@ part2(const vector& grid) return (cache[pos] = walk({ col, row + 1 })); }; - const auto col = grid[0].find('S'); - cout << "Part 2: " << walk(Pos{ col, 0 }) << '\n'; + cout << "Part 2: " << walk(Pos{ grid[0].find('S'), 0 }) << '\n'; } } // namespace -- cgit v1.3