aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2024/src/day18.cpp43
1 files changed, 38 insertions, 5 deletions
diff --git a/2024/src/day18.cpp b/2024/src/day18.cpp
index fedd836..b77650a 100644
--- a/2024/src/day18.cpp
+++ b/2024/src/day18.cpp
@@ -26,8 +26,8 @@ read_file(string_view filename)
26 return data; 26 return data;
27} 27}
28 28
29void 29optional<long>
30part1(const vector<pos_type>& data, long size) 30bfs(const vector<pos_type>& data, long size)
31{ 31{
32 set<pos_type> stones{ data.begin(), data.end() }; 32 set<pos_type> stones{ data.begin(), data.end() };
33 33
@@ -44,8 +44,7 @@ part1(const vector<pos_type>& data, long size)
44 const auto [x, y] = pos; 44 const auto [x, y] = pos;
45 45
46 if ( x == size && y == size ) { 46 if ( x == size && y == size ) {
47 cout << distance << endl; 47 return distance;
48 return;
49 } 48 }
50 49
51 if ( x < 0 || y < 0 || x > size || y > size ) { 50 if ( x < 0 || y < 0 || x > size || y > size ) {
@@ -65,11 +64,45 @@ part1(const vector<pos_type>& data, long size)
65 queue.push({ { nx, ny }, distance + 1 }); 64 queue.push({ { nx, ny }, distance + 1 });
66 } 65 }
67 } 66 }
67 return nullopt;
68}
69
70void
71part1(const vector<pos_type>& data, long size)
72{
73 if ( auto result = bfs(data, size) ) {
74 cout << *result << endl;
75 }
76}
77
78void
79part2(const vector<pos_type>& data, long size)
80{
81 for ( size_t i = 0; i != data.size(); ++i ) {
82 const vector<pos_type> foo{ data.begin(), data.begin() + ptrdiff_t(i) };
83
84 if ( !bfs(foo, size) ) {
85 const auto& [x, y] = foo.back();
86 cout << x << "," << y << endl;
87 return;
88 }
89 }
68} 90}
69 91
70int 92int
71main() 93main()
72{ 94{
95#if 0
96 const auto data = read_file("data/day18-sample1.txt");
97 const long size = 6;
98
99 part1({ data.begin(), data.begin() + 12 }, size);
100 part2(data, size);
101#else
73 const auto data = read_file("data/day18.txt"); 102 const auto data = read_file("data/day18.txt");
74 part1({ data.begin(), data.begin() + 1024 }, 70); 103 const long size = 70;
104
105 part1({ data.begin(), data.begin() + 1024 }, size);
106 part2(data, size);
107#endif
75} 108}