-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.h
392 lines (322 loc) · 12.5 KB
/
day11.h
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#ifndef DAY11_H
#define DAY11_H
#include <algorithm>
#include <cassert>
#include <list>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
class Item {
public:
enum class itemType {
// This will also be sorting order so keep alphabetical.
CHIP, GEN
};
private:
itemType type;
std::string name;
public:
Item( itemType t, std::string n ) : type( t ), name( n ) {};
itemType getType() const { return type; }
std::string getName() const { return name; }
std::string toString() const {
std::string s( name );
if ( type == itemType::GEN ) {
s += "-gen";
} else {
s += "-chip";
}
return s;
}
friend bool operator<( const Item& l, const Item& r ) {
return std::tie( l.name, l.type )
< std::tie( r.name, r.type );
}
friend bool operator==( const Item& l, const Item& r ) {
return std::tie( l.name, l.type )
== std::tie( r.name, r.type );
}
friend bool operator!=( const Item& l, const Item& r ) {
return ! ( l == r);
}
};
class Floor {
std::set<Item> items;
unsigned numGenerators = 0;
unsigned numChips = 0;
public:
void insert( Item i ) {
items.insert( i );
if ( i.getType() == Item::itemType::GEN ) {
numGenerators ++;
} else {
numChips ++;
}
}
void remove( const Item& i ) {
unsigned numErased = items.erase( i );
assert( numErased == 1 );
if ( i.getType() == Item::itemType::GEN ) {
numGenerators --;
} else {
numChips --;
}
}
size_t size() const { return items.size(); }
const std::set<Item>& getItemSet() const { return items; }
std::string toString() const {
std::ostringstream floorContents;
for ( const auto& item : items ) {
if ( floorContents.str().size() ) floorContents << " ";
floorContents << item.toString();
}
return floorContents.str();
}
// Create a string representation of a floor with normalized names. This
// will be used as a map or set key to prune floors we have already seen.
//
// It turns out that multiple floor combinations have the same number of
// steps to the final solutions so we can normalize the names for pruning.
//
// For example, the following two sets of floors both take the same amount
// of steps to complete, just the the a and b devices are swapped.
//
// floor 1: a-gen a-chip
// floor 2: b-gen b-chip
//
// floor 1: b-gen b-chip
// floor 2: a-gen a-chip
//
// The caller needs to pass an empty map when serializing the first Floor
// and call us with the same map for each additional floor that is
// serialized.
//
// The normalized floor names will be change the item names to numbers.
//
std::string toNormalizedString(
std::map<std::string,std::string>& normalizePairs ) const {
std::ostringstream floorContents;
for ( const auto& item : items ) {
if ( floorContents.str().size() ) floorContents << " ";
// NB. This won't insert if it already exists.
auto p = normalizePairs.insert( { item.getName(),
std::to_string( normalizePairs.size() ) } );
Item normalizedItem( item.getType(), (p.first)->second );
floorContents << normalizedItem.toString();
}
return floorContents.str();
}
bool isSafe() const {
for ( const auto& item : this->items ) {
if ( item.getType() == Item::itemType::CHIP ) {
Item gen( Item::itemType::GEN, item.getName() );
// Chip's generator exists. Chip is safe.
if ( this->items.find( gen ) != items.end() ) continue;
// Other generators exist, chip is not safe.
if ( this->numGenerators != 0 ) return false;
}
}
return true;
}
// Generate all possible 1 and 2 item combinations to load on the elevator.
std::set<Floor> generateElevatorCombos() const {
// NB. itemsOnThisFloor is sorted because items is a sorted set.
std::vector<Item> itemsOnThisFloor( items.begin(), items.end() );
// Each item can be sent alone in the elevator.
std::set<Floor> elevatorCombos;
for ( const auto& i : itemsOnThisFloor ) {
Floor elevatorContents;
elevatorContents.insert( i );
elevatorCombos.insert( elevatorContents );
}
// Generate pairs of items to send in the elevator.
if ( itemsOnThisFloor.size() > 1 ) {
for ( auto iterFirst = itemsOnThisFloor.cbegin() ;
iterFirst != ( itemsOnThisFloor.cend() - 1 ); ++iterFirst ) {
for ( auto iterSecond = iterFirst + 1;
iterSecond != itemsOnThisFloor.cend(); ++iterSecond ) {
Floor elevatorContents;
elevatorContents.insert( *iterFirst );
elevatorContents.insert( *iterSecond );
elevatorCombos.insert( elevatorContents );
}
}
}
return elevatorCombos;
}
friend bool operator<( const Floor& l, const Floor& r ) {
std::string ls = l.toString();
std::string rs = r.toString();
return ls < rs;
}
friend bool operator==( const Floor& l, const Floor& r ) {
std::string ls = l.toString();
std::string rs = r.toString();
return ls == rs;
}
friend bool operator!=( const Floor& l, const Floor& r ) {
return ! ( l == r );
}
};
class ContainmentFloors {
unsigned numSteps = 0;
unsigned elevatorFloor = 0;
std::vector<Floor> floors;
public:
ContainmentFloors( unsigned ef ) : elevatorFloor( ef ) {}
void parse( std::istream& is ) {
const std::regex reChip( "([[:alpha:]]+)-compatible microchip" );
const std::regex reGenerator( "([[:alpha:]]+) generator" );
unsigned floorNum = 0;
std::string line;
while ( getline( is, line ) ) {
if ( floors.size() < floorNum + 1 ) {
floors.resize( floorNum + 1 );
}
std::string lineCopy( line );
std::smatch match;
while( std::regex_search( lineCopy, match, reChip ) ) {
floors[ floorNum ].insert( { Item::itemType::CHIP, match[1] } );
lineCopy = match.suffix().str();
}
lineCopy = line;
while( std::regex_search( lineCopy, match, reGenerator ) ) {
floors[ floorNum ].insert( { Item::itemType::GEN, match[1] } );
lineCopy = match.suffix().str();
}
floorNum++;
}
}
unsigned getNumSteps() const { return numSteps; }
void incNumSteps() { numSteps++; }
unsigned getElevatorFloor() const { return elevatorFloor; }
void setElevatorFloor( unsigned floor ) { elevatorFloor = floor; }
unsigned getNumFloors() const { return floors.size(); }
Floor getFloor( unsigned floorNum ) const {
return floors[ floorNum ];
}
void setFloor( unsigned floorNum, Floor floor ) {
floors[ floorNum ] = floor;
}
bool allItemsOnTopFloor() const {
assert( floors.size() != 0 );
auto topFloorIter = floors.cend() - 1;
for ( auto iter = floors.cbegin(); iter != topFloorIter; ++iter ) {
if ( iter->size() != 0 ) return false;
}
return true;
}
std::string toString() const {
std::ostringstream out;
out << elevatorFloor << "\n";
for ( unsigned floorNum = 0; floorNum < floors.size(); floorNum ++ ) {
out << floorNum << " ";
out << floors[ floorNum ].toString() << "\n";
}
return out.str();
}
// Create string representation with normalized item names. See
// Floor::toNormalizedString() comments for more details about this.
std::string toNormalizedString() const {
std::map<std::string,std::string> normalizePairs;
std::ostringstream out;
out << elevatorFloor << "\n";
for ( unsigned floorNum = 0; floorNum < floors.size(); floorNum ++ ) {
out << floorNum << " ";
out << floors[ floorNum ].toNormalizedString( normalizePairs )
<< "\n";
}
return out.str();
}
// Are all floors safe?
bool isSafe() const {
for ( const auto& floor : floors ) {
if ( ! floor.isSafe() ) {
return false;
}
}
return true;
}
};
// Perform a breadth first simulation of all possible moves required to move
// everything safely to the top floor.
class Simulate {
std::set<std::string> alreadySimulated;
public:
unsigned simulate( const ContainmentFloors& initialFloors ) {
std::list<ContainmentFloors> floorQueue;
floorQueue.push_back( initialFloors );
std::string s = initialFloors.toNormalizedString();
alreadySimulated.insert( s );
while ( ! floorQueue.empty() ) {
auto iter = floorQueue.cbegin();
if ( iter->allItemsOnTopFloor() ) {
return iter->getNumSteps();
}
// Generate elevator contents
unsigned elevatorFloorNum = iter->getElevatorFloor();
Floor elevatorFloorItems = iter->getFloor( elevatorFloorNum );
std::set<Floor> elevatorCombos =
elevatorFloorItems.generateElevatorCombos();
// For each elevator contents, send to above and below floors (if
// floor exists).
unsigned numFloors = iter->getNumFloors();
for ( const auto& combo : elevatorCombos ) {
if ( elevatorFloorNum != 0 ) {
ContainmentFloors newCF( *iter );
newCF.incNumSteps();
unsigned nextFloorNum = elevatorFloorNum - 1;
Floor elevatorFloorItems = newCF.getFloor( elevatorFloorNum );
Floor nextFloorItems = newCF.getFloor( nextFloorNum );
for ( const auto& item : combo.getItemSet() ) {
nextFloorItems.insert( item );
elevatorFloorItems.remove( item );
}
newCF.setFloor( nextFloorNum, nextFloorItems );
newCF.setFloor( elevatorFloorNum, elevatorFloorItems );
newCF.setElevatorFloor( nextFloorNum );
// Insert the new floor set into the simulation queue if
// we haven't seen it or one of its aliases.
std::string s = newCF.toNormalizedString();
if ( newCF.isSafe() ) {
auto p = alreadySimulated.insert( s );
if ( p.second == true ) {
floorQueue.push_back( newCF );
}
}
}
if ( elevatorFloorNum != numFloors - 1 ) {
ContainmentFloors newCF( *iter );
newCF.incNumSteps();
unsigned nextFloorNum = elevatorFloorNum + 1;
Floor elevatorFloorItems = newCF.getFloor( elevatorFloorNum );
Floor nextFloorItems = newCF.getFloor( nextFloorNum );
for ( const auto& item : combo.getItemSet() ) {
nextFloorItems.insert( item );
elevatorFloorItems.remove( item );
}
newCF.setFloor( nextFloorNum, nextFloorItems );
newCF.setFloor( elevatorFloorNum, elevatorFloorItems );
newCF.setElevatorFloor( nextFloorNum );
// Insert the new floor set into the simulation queue if
// we haven't seen it or one of its aliases.
std::string s = newCF.toNormalizedString();
if ( newCF.isSafe() ) {
auto p = alreadySimulated.insert( s );
if ( p.second == true ) {
floorQueue.push_back( newCF );
}
}
}
}
floorQueue.pop_front();
}
// We shouldn't get here.
assert( ! "Got to invalid location" );
return 0;
}
};
#endif // DAY11_H