-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_15.cpp
187 lines (148 loc) · 5.09 KB
/
day_15.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//---------------------------------------------------------------------------//
//#include <string>
//#include <iostream>
//#include <sstream>
//#include <map>
//#include <unordered_map>
//#include <vector>
#include <ranges>
//#include <fstream>
//#include <limits>
//#include <set>
//#include <iomanip>
//#include <queue>
//#include <algorithm>
//---------------------------------------------------------------------------//
#include "day"
//---------------------------------------------------------------------------//
namespace aoc::YEAR::DAY {
//---------------------------------------------------------------------------//
// template<class T>
// constexpr auto Range ( T a, T b ) {
// return std::views::iota ( a, b );
// }
//---------------------------------------------------------------------------//
auto load ( std::istream& fs ) {
//std::fstream fs ( file );
std::vector<std::string> data;
std::string line;
while ( fs >> line ) {
data.push_back ( line );
}
return data;
}
//---------------------------------------------------------------------------//
std::unordered_map<int, uint64_t> memo;
//---------------------------------------------------------------------------//
uint64_t traverse ( std::vector<std::string>& d, uint64_t x = 0, uint64_t y = 0 ) {
if ( x == d.size() - 1 && y == d.size() - 1 ) return d[y][x] - '0';
if ( x >= d.size() || y >= d.size() ) return -1;
auto i = ( y << 16 ) | x;
if ( memo.contains ( i ) ) return memo[i];
auto s1 = traverse ( d, x + 1, y );
uint64_t s2 = traverse ( d, x, y + 1 );
return memo[i] = std::min ( s1, s2 ) + d[y][x] - '0';
}
//---------------------------------------------------------------------------//
uint64_t traverse2 ( std::vector<std::string>& d ) {
std::vector<int> a;
int width = d.size();
for ( auto& l : d ) {
for ( auto c : l ) a.push_back ( c - '0' );
//a[y*width+x++] = c-'0';
}
for ( auto i = width - 1; i > 0; i-- ) {
a[ ( i - 1 ) *width + width - 1] += a[i * width + width - 1 ];
a[ ( width - 1 ) *width + i - 1] += a[ ( width - 1 ) * width + i];
}
for ( auto y = width - 2; y >= 0 ; y-- ) {
for ( auto x = width - 2; x >= 0; x-- ) {
auto right = a[ y * width + x + 1];
auto down = a[ ( y + 1 ) * width + x ];
a[ y * width + x ] += std::min ( right, down );
}
}
return a[0];
}
//---------------------------------------------------------------------------//
struct Q {
int d = 0, x = 0, y = 0;
};
bool operator < ( const Q& a, const Q& b ) {
return a.d > b.d;
}
//---------------------------------------------------------------------------//
int dijkstra ( std::vector<std::string>& d ) {
std::vector<Q> q; // kolejka
const int N = d.size();
std::vector<std::vector<int>> dist ( N );
for ( auto& d : dist )
d.resize ( N, INT32_MAX );
dist[0][0] = 0;
Q c;
auto cmp = [ & ] ( int a, int b ) {
auto l = dist[c.y][c.x] + d[c.y + b][c.x + a] - '0';
if ( l < dist[c.y + b][c.x + a] ) {
dist[c.y + b][c.x + a] = l;
q.push_back ( { l, c.x + a, c.y + b} );
}
};
do {
if ( c.x + 1 < N ) cmp ( 1, 0 );
if ( c.x - 1 >= 0 ) cmp ( -1, 0 );
if ( c.y + 1 < N ) cmp ( 0, 1 );
if ( c.y - 1 >= 0 ) cmp ( 0, -1 );
std::sort ( q.begin(), q.end() );
c = q.back();
if ( c.x == N - 1 && c.y == N - 1 ) return dist[N - 1][N - 1];
q.pop_back ();
} while ( q.size() );
return dist[N - 1][N - 1];
}
//---------------------------------------------------------------------------//
std::string inc ( std::string s ) {
for ( auto& c : s )
if ( ++c > '9' ) c = '1';
return s;
}
//---------------------------------------------------------------------------//
void inc ( std::vector<std::string>& d ) {
for ( auto& l : d )
l = inc ( l );
}
//---------------------------------------------------------------------------//
void Task_1 ( std::istream& puzzle_input ) {
auto ans = 0ull;
auto data = load ( puzzle_input
//"../test2input"
);
/*for ( auto& l: data )
std::cout << l << '\n';
*/
memo.clear();
ans = traverse2 ( data ) - data[0][0] + '0';
std::cout << __FUNCTION__ << ": " << ans << '\n';
}
//---------------------------------------------------------------------------//
void Task_2 ( std::istream& puzzle_input ) {
auto ans = 0ull;
auto data = load ( puzzle_input
//"../input"
//"../test2input"
);
memo.clear();
for ( auto& l : data ) {
l += inc ( l + inc ( l + inc ( l + inc ( l ) ) ) );
}
std::vector<std::string> nd;
for ( auto i : Range ( 0, 5 ) ) {
(void)i;
nd.insert ( nd.end(), data.begin(), data.end() );
inc ( data );
}
ans = dijkstra ( nd );
std::cout << __FUNCTION__ << ": " << ans << '\n';
}
//---------------------------------------------------------------------------//
}
//---------------------------------------------------------------------------//