-
Notifications
You must be signed in to change notification settings - Fork 0
/
16b.cpp
148 lines (132 loc) · 3.39 KB
/
16b.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
#include "parse_input.h"
#include <algorithm>
#include <array>
#include <numeric>
enum dir
{
up,
down,
left,
right
};
int nx( int x, dir moving )
{
switch( moving )
{
case up:
case down:
return x;
case left:
return x - 1;
case right:
return x + 1;
}
}
int ny( int y, dir moving )
{
switch( moving )
{
case up:
return y - 1;
case down:
return y + 1;
case left:
case right:
return y;
}
}
struct from_dir
{
std::array< bool, 4 > dir{};
};
std::size_t count_energized( std::vector< from_dir > const& row )
{
return std::ranges::count_if( row, []( from_dir const& tile ) { return tile.dir[ up ] || tile.dir[ down ] || tile.dir[ left ] || tile.dir[ right ]; } );
}
struct beams
{
beams( std::vector< std::string > const& the_input, int x, int y, dir moving ) :
input( the_input ),
rays( input.size(), std::vector< from_dir >( input.front().size() ) )
{
go( x, y, moving );
}
std::vector< std::string > const& input;
std::vector< std::vector< from_dir > > rays;
void go( int x, int y, dir moving )
{
int x1 = nx( x, moving );
int y1 = ny( y, moving );
if( x1 < 0 || x1 >= rays.front().size() || y1 < 0 || y1 >= rays.size() || rays[ y1 ][ x1 ].dir[ moving ] )
{
return;
}
rays[ y1 ][ x1 ].dir[ moving ] = true;
auto const tile = input[ y1 ][ x1 ];
switch( tile )
{
case '.':
return go( x1, y1, moving );
case '|':
if( moving == left || moving == right )
{
go( x1, y1, up );
go( x1, y1, down );
return;
}
return go( x1, y1, moving );
case '-':
if( moving == left || moving == right )
return go( x1, y1, moving );
else
{
go( x1, y1, left );
go( x1, y1, right );
return;
}
case '/':
switch( moving )
{
case up:
return go( x1, y1, right );
case down:
return go( x1, y1, left );
case left:
return go( x1, y1, down );
case right:
return go( x1, y1, up );
}
case '\\':
switch( moving )
{
case up:
return go( x1, y1, left );
case down:
return go( x1, y1, right );
case left:
return go( x1, y1, up );
case right:
return go( x1, y1, down );
}
}
}
std::size_t count()
{
return std::transform_reduce( rays.cbegin() + 1, rays.cend(), count_energized( rays.front() ), std::plus<>(), count_energized );
}
};
advent_t advent( std::vector< std::string > const& input )
{
std::size_t max{};
for( int x = 0; x < input.front().size(); ++x )
{
max = std::max( max, beams( input, x, -1, down ).count() );
max = std::max( max, beams( input, x, input.size(), up ).count() );
}
for( int y = 0; y < input.size(); ++y )
{
max = std::max( max, beams( input, -1, y, right ).count() );
max = std::max( max, beams( input, input.front().size(), y, left ).count() );
}
return max;
}