-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.cpp
171 lines (156 loc) · 3.55 KB
/
Day16.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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
int dir2index( int di, int dj )
{
if( di == 1 ){ return 0; }
if( di == -1 ){ return 1; }
if( dj == 1 ){ return 2; }
if( dj == -1 ){ return 3; }
return -1;
}
void bfs(
std::vector<std::string> const& board,
std::vector<std::vector<std::bitset<4>>> &status,
int i, int j,
int di, int dj
)
{
struct status_t
{
int i, j;
int di, dj;
};
std::vector<status_t> bfs, pong;
bfs.push_back( {i,j,di,dj} );
while( bfs.empty() == false )
{
pong.clear();
for( auto b : bfs )
{
// check out-of-range
if( b.i<0 || b.j<0 || b.i>=board.size() || b.j>=board[0].size() ){ continue; }
// check already visited
int dir_index = dir2index( b.di, b.dj );
if( status[b.i][b.j][ dir_index ] ){ continue; }
// set visited flag to true
status[b.i][b.j][dir_index] = true;
const status_t right = { b.i, b.j+1, 0, 1 };
const status_t left = { b.i, b.j-1, 0, -1 };
const status_t up = { b.i-1, b.j, -1, 0 };
const status_t down = { b.i+1, b.j, 1, 0 };
const status_t forward = { b.i+b.di, b.j+b.dj, b.di, b.dj };
char ch = board[b.i][b.j];
if( ch == '.' )
{
pong.push_back( forward );
}else if( ch == '|' )
{
if( b.dj != 0 )
{
// split
pong.push_back( up );
pong.push_back( down );
}else
{
// non-split
pong.push_back( forward );
}
}else if( ch == '-' )
{
if( b.di != 0 )
{
// split
pong.push_back( left );
pong.push_back( right );
}else
{
// non-split
pong.push_back( forward );
}
}else if( ch == '\\' )
{
if( b.dj == 1 )
{
// right-in, down out
pong.push_back( down );
}else if( b.dj == -1 )
{
// left-in, up out
pong.push_back( up );
}else if( b.di == 1 )
{
// down in, right out
pong.push_back( right );
}else if( b.di == -1 )
{
// up in, left out
pong.push_back( left );
}
}else if( ch == '/' )
{
if( b.dj == 1 )
{
// right-in, up out
pong.push_back( up );
}else if( b.dj == -1 )
{
// left-in, down out
pong.push_back( down );
}else if( b.di == 1 )
{
// down in, left out
pong.push_back( left );
}else if( b.di == -1 )
{
// up in, right out
pong.push_back( right );
}
}
}
std::swap( bfs, pong );
}
}
int main()
{
/*
std::string str =
".|...\\....\n"
"|.-.\\.....\n"
".....|-...\n"
"........|.\n"
"..........\n"
".........\\\n"
"..../.\\\\..\n"
".-.-/..|..\n"
".|....-|.\\\n"
"..//.|....";
std::istringstream input_file( str );
*/
std::ifstream input_file( "../inputs/Day16.txt" );
std::vector<std::string> board;
std::vector<std::vector<std::bitset<4>>> status;
std::string line;
while( std::getline(input_file,line) )
{
status.emplace_back();
status.back().resize( line.size(), 0 );
std::cout << line << "\n";
board.push_back( std::move(line) );
}
// shoot right from (0,0)
bfs( board, status, 0, 0, 0, 1 );
int answer = 0;
for( auto &l : status )
{
for( auto x : l )
{
std::cout << x.count();
if( x.any() ){ answer++; }
}
std::cout << "\n";
}
std::cout << answer << "\n";
}