-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.cpp
91 lines (84 loc) · 1.86 KB
/
Day13.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
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
std::vector<std::string> transpose( std::vector<std::string> const& board )
{
std::vector<std::string> ret( board[0].size() );
for( int i=0; i<ret.size(); ++i )
{
ret[i].resize( board.size() );
for( int j=0; j<board.size(); ++j )
{
ret[i][j] = board[j][i];
}
}
return ret;
}
int search_row_reflect( std::vector<std::string> const& board )
{
// row-reflection check
for( int i=0; i<board.size()-1; ++i )
{
if( board[i] == board[i+1] )
{
int i0 = i;
bool find = true;
int j = i+1;
while( i>=0 && j<board.size() )
{
if( board[i] != board[j] ){ find=false; break; }
--i;
++j;
}
if( find )
{
return i0;
}
i = i0;
}
}
return -1;
}
std::pair<int,int> solve( std::vector<std::string> const& board )
{
int row = search_row_reflect(board);
if( row != -1 ){ return {0,row}; }
auto tr = transpose(board);
return {search_row_reflect(board),0};
}
int main()
{
std::ifstream input_file( "../inputs/Day13.txt" );
std::string line;
std::vector<std::string> board;
int answer = 0;
int col_reflect = 0;
int row_reflect = 0;
while( std::getline(input_file,line) )
{
if( line.size() == 0 )
{
for( auto &s : board )
{
std::cout << s << "\n";
}
int row = search_row_reflect(board);
if( row != -1 )
{
row_reflect += row+1;
std::cout << row << "\n";
}else {
auto tr = transpose(board);
int col = search_row_reflect(tr);
if( col == -1 ){ std::cout << "what?\n"; }
col_reflect += col+1;
std::cout << col << "\n";
}
board.clear();
continue;
}
board.push_back( std::move(line) );
}
std::cout << row_reflect*100 + col_reflect << "\n";
}