aboutsummaryrefslogtreecommitdiff
path: root/2025/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-12-04 18:17:43 +0100
committerThomas Schmucker <ts@its1.de>2025-12-04 18:17:43 +0100
commitd681bdf26861b2bbe3b4c206b1a7a4930d0d5198 (patch)
tree0eb33da083f721eb6634afaebb7edfe0fd69a408 /2025/src
parentacb278e7a61b13d14767e3a84c75d4f9f0ca6e7a (diff)
downloadadvent-of-code-d681bdf26861b2bbe3b4c206b1a7a4930d0d5198.tar.gz
advent-of-code-d681bdf26861b2bbe3b4c206b1a7a4930d0d5198.tar.bz2
advent-of-code-d681bdf26861b2bbe3b4c206b1a7a4930d0d5198.zip
aoc 2025, day 4, code cleanup
Diffstat (limited to '2025/src')
-rw-r--r--2025/src/day04.cpp33
1 files changed, 16 insertions, 17 deletions
diff --git a/2025/src/day04.cpp b/2025/src/day04.cpp
index 295ed75..ef3b7f5 100644
--- a/2025/src/day04.cpp
+++ b/2025/src/day04.cpp
@@ -1,6 +1,9 @@
1#include <array>
1#include <filesystem> 2#include <filesystem>
2#include <fstream> 3#include <fstream>
3#include <iostream> 4#include <iostream>
5#include <map>
6#include <queue>
4#include <set> 7#include <set>
5#include <string> 8#include <string>
6#include <tuple> 9#include <tuple>
@@ -30,7 +33,7 @@ read_file(const filesystem::path& filename)
30 return grid; 33 return grid;
31} 34}
32 35
33set<Pos> 36array<Pos, 8>
34get_neighbours(const Pos& pos) 37get_neighbours(const Pos& pos)
35{ 38{
36 auto [col, row] = pos; 39 auto [col, row] = pos;
@@ -47,40 +50,35 @@ get_neighbours(const Pos& pos)
47 }; 50 };
48} 51}
49 52
53size_t
54get_nneighbours(const set<Pos>& grid, const Pos& pos)
55{
56 return (size_t) ranges::count_if(get_neighbours(pos), [&](const auto& neighbour) { return grid.contains(neighbour); });
57}
58
50bool 59bool
51is_accessable(const set<Pos>& grid, const Pos& pos) 60is_accessable(const set<Pos>& grid, const Pos& pos)
52{ 61{
53 long nneighbours = 0; 62 return get_nneighbours(grid, pos) < 4;
54 for ( const auto neighbour: get_neighbours(pos) ) {
55 if ( grid.contains(neighbour) ) {
56 ++nneighbours;
57 }
58 }
59 return nneighbours < 4;
60} 63}
61 64
62void 65void
63part1(const set<Pos>& grid) 66part1(const set<Pos>& grid)
64{ 67{
65 long count = 0; 68 auto count = ranges::count_if(grid, [&](const auto& pos) { return is_accessable(grid, pos); });
66 for ( const auto pos: grid ) {
67 if ( is_accessable(grid, pos) ) {
68 ++count;
69 }
70 }
71 cout << "Part 1: " << count << '\n'; 69 cout << "Part 1: " << count << '\n';
72} 70}
73 71
74void 72void
75part2(set<Pos> grid) 73part2(set<Pos> grid)
76{ 74{
77 auto old_size = grid.size(); 75 const auto old_size = grid.size();
78 while ( true ) { 76 while ( true ) {
79 set<Pos> remove; 77 vector<Pos> remove;
80 78
81 for ( const auto& pos: grid ) { 79 for ( const auto& pos: grid ) {
82 if ( is_accessable(grid, pos) ) { 80 if ( is_accessable(grid, pos) ) {
83 remove.insert(pos); 81 remove.emplace_back(pos);
84 } 82 }
85 } 83 }
86 84
@@ -92,6 +90,7 @@ part2(set<Pos> grid)
92 grid.erase(pos); 90 grid.erase(pos);
93 } 91 }
94 } 92 }
93
95 cout << "Part 2: " << old_size - grid.size() << '\n'; 94 cout << "Part 2: " << old_size - grid.size() << '\n';
96} 95}
97 96