-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_03.cpp
96 lines (80 loc) · 2.77 KB
/
day_03.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
#include <day>
//---------------------------------------------------------------------------//
namespace aoc::YEAR::DAY {
//---------------------------------------------------------------------------//
constexpr uint N=1000;
//---------------------------------------------------------------------------//
std::istringstream test_input{
R"(#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2
)"};
//---------------------------------------------------------------------------//
struct S{
uint16_t id, x, y, w, h;
uint top(){ return y;}
uint bottom(){ return y+h;}
uint left(){ return x;}
uint right(){ return x+w;}
uint area(){ return w*h; }
};
//---------------------------------------------------------------------------//
auto load ( ::std::istream& file ) {
::std::vector<S> ret;
std::string line;
while( std::getline ( file, line ) ) {
std::istringstream l(line);
uint16_t id,x,y,w,h;
char c;
l >> c >> id >> c >> x >> c >> y >> c >> w >> c >> h;
ret.push_back ( { id, x, y, w, h } );
}
return ret;
}
//---------------------------------------------------------------------------//
void Task_1 ( std::istream& puzzle_input ) {
//aoc::test_enable();
auto& file = aoc::is_test_enabled() ? test_input : puzzle_input;
auto data = load ( file );
auto ans = 0ull;
std::array<char,N*N> tab{0};
for ( auto& rec : data )
for ( uint y=rec.top(); y < rec.bottom() ; y++ )
for ( uint x=rec.left(); x < rec.right() ; x++ )
if ( tab[y*N+x] < 2 ) {
tab[y*N+x] += 1;
if ( tab[y*N+x] == 2 )
ans++;
}
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::array<char, N*N> tab{0};
for ( auto& rec : data )
for ( uint y=rec.top(); y < rec.bottom(); y++ )
for ( uint x=rec.left(); x < rec.right(); x++ )
if ( tab[y*N+x] < 2 ) {
tab[y*N+x] += 1;
if ( tab[y*N+x] == 2 )
ans++;
}
for ( auto& rec : data ) {
uint area = 0;
for ( uint y=rec.top(); y < rec.bottom(); y++ )
for ( uint x=rec.left(); x < rec.right(); x++ )
area += tab[y*N+x];
if ( area == rec.area() ) {
ans = rec.id;
break;
}
}
OUT(ans);
}
//---------------------------------------------------------------------------//
}
//---------------------------------------------------------------------------//