-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_08.cpp
136 lines (116 loc) · 3.88 KB
/
day_08.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
#include "day"
struct P {
int x,y;
bool operator==( const P&) const = default;
P operator-( const P& o ){ return { x-o.x, y-o.y }; }
void operator+=( const P& o ){ x+=o.x; y+=o.y; }
void operator-=( const P& o ){ x-=o.x; y-=o.y; }
size_t hash() const { return (((size_t)y)<<32) + x; }
};
template <> struct std::hash<P> {
size_t operator() ( const P& p ) const noexcept {
return p.hash ( );
}
};
//---------------------------------------------------------------------------//
namespace aoc::YEAR::DAY {
//---------------------------------------------------------------------------//
// Test data
std::string testinput (
R"(............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
)");
//---------------------------------------------------------------------------//
inline std::istream& test_input() {
static std::stringstream ss;
return ss = std::stringstream ( testinput );;
}
//---------------------------------------------------------------------------//
auto load( std::istream& file ) {
::std::vector<std::string> ret;
::std::string line;
while ( ::std::getline( file, line ) ) {
ret.push_back ( line );
}
return ret;
}
//---------------------------------------------------------------------------//
void Task_1 ( ::std::istream& puzzle_input ) {
auto ans=0ull;
//aoc::test_enable();
auto& file = aoc::is_test_enabled() ? test_input() : puzzle_input;
auto data = load ( file );
//std::cout << data.size() << 'x' << data[0].size() << '\n';
std::unordered_map<char,std::vector<P>> antena;
const auto n = (int)data.size();
for ( int y=0; y < n; y++ ) {
//std::cout << data[y] << '\n';
for ( int x=0; x < n; x++ ) {
char c = data[y][x];
if ( c!='.') antena[c].push_back({x,y});
}
}
std::unordered_set<P> antinode;
for ( auto it : antena )
for ( uint i=0; i < it.second.size(); i++ )
for ( uint j=i+1; j < it.second.size(); j++ ) {
auto p1 = it.second[i];
auto p2 = it.second[j];
auto d = p2 - p1;
p1 -= d;
p2 += d;
if ( p1.x>=0 && p1.x<n && p1.y>=0 && p1.y<n )
antinode.insert ( p1 );
if ( p2.x>=0 && p2.x<n && p2.y>=0 && p2.y<n )
antinode.insert ( p2 );
}
ans = antinode.size();
OUT ( ans );
}
//---------------------------------------------------------------------------//
void Task_2 ( ::std::istream& puzzle_input ) {
auto ans = 0ull;
//aoc::test_enable();
auto& file = aoc::is_test_enabled() ? test_input() : puzzle_input;
auto data = load( file );
std::unordered_map<char,std::vector<P>> antena;
const auto n = (int)data.size();
for ( int y=0; y < n; y++ ) {
//std::cout << data[y] << '\n';
for ( int x=0; x < n; x++ ) {
char c = data[y][x];
if ( c!='.') antena[c].push_back({x,y});
}
}
std::unordered_set<P> antinode;
for ( auto it : antena )
for ( uint i=0; i < it.second.size(); i++ )
for ( uint j=i+1; j < it.second.size(); j++ ) {
auto p1 = it.second[i];
auto p2 = it.second[j];
auto d = p2 - p1;
while ( p1.x>=0 && p1.x<n && p1.y>=0 && p1.y<n ) {
antinode.insert ( p1 );
p1-=d;
}
while ( p2.x>=0 && p2.x<n && p2.y>=0 && p2.y<n ) {
antinode.insert ( p2 );
p2+=d;
}
}
ans = antinode.size();
OUT ( ans );
}
//---------------------------------------------------------------------------//
}
//---------------------------------------------------------------------------//