-
Notifications
You must be signed in to change notification settings - Fork 0
/
23b.cpp
137 lines (129 loc) · 4.26 KB
/
23b.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
#include "parse_input.h"
#include <algorithm>
#include <array>
#include <map>
#include <numeric>
#include <ranges>
using weighted_graph = std::map< char, std::map< char, int > >;
int go( weighted_graph const& graph, char const from, char const to, int const overhead )
{
int best{};
for( auto const& [ next_k, len ] : graph.at( from ) )
{
if( next_k == to )
{
best = std::max( best, overhead + len );
continue;
}
auto tmp_graph = graph;
tmp_graph.erase( from );
for( auto const& thing : graph.at( from ) )
{
tmp_graph.at( thing.first ).erase( from );
}
auto tmp_overhead = overhead + len;
best = std::max( best, go( tmp_graph, next_k, to, tmp_overhead ) );
}
return best;
}
advent_t advent( std::vector< std::string > const& input )
{
std::map< char, std::pair< int, int > > id_to_loc;
std::map< std::pair< int, int >, char > loc_to_id;
weighted_graph you_can_go;
auto modified = input;
for( auto& line : modified )
{
std::ranges::replace( line, '>', '.' );
std::ranges::replace( line, '<', '.' );
std::ranges::replace( line, '^', '.' );
std::ranges::replace( line, 'v', '.' );
}
for( int row = 1; row < modified.size(); ++row )
{
for( int col = 1; col < modified.front().size(); ++col )
{
if( modified[ row ][ col ] == '.' )
{
int paths{};
if( modified[ row - 1 ][ col ] == '.' )
++paths;
if( modified[ row + 1 ][ col ] == '.' )
++paths;
if( modified[ row ][ col - 1 ] == '.' )
++paths;
if( modified[ row ][ col + 1 ] == '.' )
++paths;
if( paths < 3 )
continue;
modified[ row ][ col ] = 'F'; // Fork
std::pair< int, int > loc = { row, col };
char id;
if( id_to_loc.size() < 10 )
{
id = id_to_loc.size() + '0';
}
else
{
id = id_to_loc.size() + 'A' - 10;
}
id_to_loc[ id ] = loc;
loc_to_id[ loc ] = id;
}
}
}
int overhead{};
char source;
char sink;
auto wander = [ & ]( int row, int col ) {
auto tmp = modified;
int len{ 1 };
while( row > 0 && row + 1 < modified.size() && tmp[ row ][ col ] != 'F' )
{
tmp[ row ][ col ] = 'O';
++len;
if( tmp[ row - 1 ][ col ] == '.' || ( len > 2 && tmp[ row - 1 ][ col ] == 'F' ) )
--row;
else if( tmp[ row + 1 ][ col ] == '.' || ( len > 2 && tmp[ row + 1 ][ col ] == 'F' ) )
++row;
else if( tmp[ row ][ col - 1 ] == '.' || ( len > 2 && tmp[ row ][ col - 1 ] == 'F' ) )
--col;
else
++col;
}
if( row == 0 )
return std::make_pair( 'Y', len );
if( row + 1 == modified.size() )
return std::make_pair( 'Z', len );
return std::make_pair( loc_to_id[ std::make_pair( row, col ) ], len );
};
for( auto const& [ k, v ] : id_to_loc )
{
auto const tell = [ &, k = k ]( auto const row, auto const col ) {
if( modified[ row ][ col ] == '.' )
{
auto const result = wander( row, col );
if( result.first == 'Y' )
{
source = k;
overhead += result.second;
}
else if( result.first == 'Z' )
{
sink = k;
overhead += result.second;
}
else
{
you_can_go[ k ][ result.first ] = result.second;
you_can_go[ result.first ][ k ] = result.second;
}
}
};
tell( v.first - 1, v.second );
tell( v.first + 1, v.second );
tell( v.first, v.second - 1 );
tell( v.first, v.second + 1 );
}
return go( you_can_go, source, sink, overhead );
}