-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_17.cpp
207 lines (159 loc) · 5.36 KB
/
problem_17.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "AoC/2015/problem_17.h"
#include "AoC/problems_map.h"
#include "AoC/utils/ranges/2dvector.h"
#include "range/v3/action/sort.hpp"
#include "range/v3/algorithm/lexicographical_compare.hpp"
#include "range/v3/algorithm/min.hpp"
#include "range/v3/iterator/operations.hpp"
#include "range/v3/numeric/accumulate.hpp"
#include "range/v3/range/conversion.hpp"
#include "range/v3/view/any_view.hpp"
#include "range/v3/view/concat.hpp"
#include "range/v3/view/empty.hpp"
#include "range/v3/view/filter.hpp"
#include "range/v3/view/istream.hpp"
#include "range/v3/view/single.hpp"
#include "range/v3/view/tail.hpp"
#include "range/v3/view/transform.hpp"
#include "boost/numeric/conversion/cast.hpp"
#include <istream>
#include <map>
namespace
{
using Volume = int;
using ContainersCombinations = ranges::any_view<ranges::any_view<Volume>>;
using Containers = std::vector<Volume>;
using CacheKey = std::pair<Volume, Containers>;
struct CacheComparator
{
bool operator()( const CacheKey& k1, const CacheKey& k2 ) const
{
if ( k1.first != k2.first )
{
return k1.first < k2.first;
}
auto r1 = k1.second;
auto r2 = k2.second;
return ranges::lexicographical_compare( r1, r2 );
}
};
using Cache = std::map<CacheKey, ContainersCombinations, CacheComparator>;
ContainersCombinations generate_combinations( ranges::any_view<Volume> containers, const Volume volume_left, Cache& cache )
{
const auto cache_iter = cache.find( { volume_left, ranges::to<Containers>( containers ) } );
if ( cache_iter != cache.cend() )
{
return cache_iter->second;
}
if ( volume_left == 0 )
{
return ranges::views::single( ranges::views::empty<Volume> );
}
else if ( ( containers.begin() == containers.end() ) || volume_left < 0 )
{
return {};
}
auto head = *containers.begin();
if ( volume_left < head )
{
return {};
}
auto tail = containers | ranges::views::tail;
ContainersCombinations result1 =
generate_combinations( tail, volume_left - head, cache ) |
ranges::views::transform( [ head ]( auto rng ) { return ranges::views::concat( ranges::views::single( head ), rng ); } );
ContainersCombinations result2 = generate_combinations( tail, volume_left, cache );
auto result = ranges::views::concat( result1, result2 );
cache.emplace( std::make_pair( volume_left, ranges::to<Containers>( containers ) ), result );
return result;
}
ContainersCombinations generate_combinations( std::istream& input, const Volume volume_to_store )
{
auto containers = ranges::istream<Volume>( input ) | ranges::to_vector | ranges::actions::sort;
Cache cache;
return generate_combinations( containers, volume_to_store, cache );
}
size_t get_possible_combinations_num( std::istream& input, const Volume volume_to_store )
{
return boost::numeric_cast<size_t>( ranges::distance( generate_combinations( input, volume_to_store ) ) );
}
size_t get_min_containers_combinations_num( std::istream& input, const Volume volume_to_store )
{
const auto combinations = generate_combinations( input, volume_to_store ) | AoC::to_2d_vector();
const auto combination_with_min_containers =
ranges::min( combinations, []( const auto c1, const auto c2 ) { return c1.size() < c2.size(); } );
const auto min_num_of_containers = combination_with_min_containers.size();
auto combitions_with_min_containers =
combinations | ranges::views::filter( [ min_num_of_containers ]( const auto c ) { return c.size() == min_num_of_containers; } );
return boost::numeric_cast<size_t>( ranges::distance( combitions_with_min_containers ) );
}
} // namespace
namespace AoC_2015
{
namespace problem_17
{
size_t solve_1( std::istream& input )
{
return get_possible_combinations_num( input, 150 );
}
size_t solve_2( std::istream& input )
{
return get_min_containers_combinations_num( input, 150 );
}
AOC_REGISTER_PROBLEM( 2015_17, solve_1, solve_2 );
} // namespace problem_17
} // namespace AoC_2015
#ifndef NDEBUG
# include "impl_tests.h"
# include <cassert>
# include <sstream>
static void impl_tests()
{
using VV = std::vector<Containers>;
{
const std::vector<Volume> v{};
Cache cache;
auto r = generate_combinations( v, 1, cache ) | AoC::to_2d_vector();
assert( r == VV{} );
}
{
const std::vector<Volume> v{ 1 };
Cache cache;
auto r = generate_combinations( v, 0, cache ) | AoC::to_2d_vector();
assert( r == VV{ {} } );
}
{
const std::vector<Volume> v{ 3 };
Cache cache;
auto r = generate_combinations( v, 1, cache ) | AoC::to_2d_vector();
assert( r == VV{} );
}
{
const std::vector<Volume> v{ 3 };
Cache cache;
auto r = generate_combinations( v, 3, cache ) | AoC::to_2d_vector();
assert( r == VV{ { 3 } } );
}
{
const std::vector<Volume> v{ 2, 3, 5 };
Cache cache;
auto r = generate_combinations( v, 10, cache ) | AoC::to_2d_vector();
assert( r == ( VV{ { 2, 3, 5 } } ) );
}
{
const std::vector<Volume> v{ 5, 5, 100, 100 };
Cache cache;
auto r = generate_combinations( v, 10, cache ) | AoC::to_2d_vector();
assert( r == ( VV{ { 5, 5 } } ) );
}
{
std::istringstream ss( "20 15 10 5 5" );
assert( 4 == get_possible_combinations_num( ss, 25 ) );
}
{
std::istringstream ss( "20 15 10 5 5" );
assert( 3 == get_min_containers_combinations_num( ss, 25 ) );
}
}
REGISTER_IMPL_TEST( impl_tests );
#endif