Repository navigation
Expand file tree
/
Copy pathAssignment14.cpp
More file actions
165 lines (148 loc) · 3.6 KB
/
Copy pathAssignment14.cpp
File metadata and controls
165 lines (148 loc) · 3.6 KB
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
#include "AdventClass.h"
#include "FileUtility.h"
#include "LimitFunctions.h"
#include "StringFunctions.h"
#include "StringHandler.h"
class TestAssignment14 : public TAdventClass
{
public:
class TGrid : public TCVector<TCVector<wxInt32>>
{
public:
wxInt32 Value(wxInt32 x, wxInt32 y) {
return Value({ y, x });
}
wxInt32 Value(wxPoint Pos) const {
return at(Pos.y).at(Pos.x);
}
wxInt32 Columns() const {
return front().size();
}
wxInt32 Rows() const {
return size();
}
void Set(wxPoint Pos, wxInt32 Value) {
at(Pos.y).at(Pos.x) = Value;
}
};
};
TEST_F(TestAssignment14, TestSamples)
{
TGrid Grid;
auto Point = [](wxString Input)
{
auto Xy = Split(Input, ",");
return wxPoint(StringToInt<wxInt32>(Xy[0]), StringToInt<wxInt32>(Xy[1]));
};
auto MaxY = 0;
Grid.resize(3000, TCVector<wxInt32>(3000, wxNOT_FOUND));
for (auto& Line : ReadFileLines(RealInput()))
{
auto Tokens = Split(Line, ">");
for (auto Index = 1; Index < Tokens.size(); ++Index)
{
auto To = Point(Tokens.at(Index));
auto From = Point(Tokens.at(Index - 1));
From.x += 1000;
To.x += 1000;
Grid.Set(From, 1);
Grid.Set(To, 1);
MaxY = Maximum(From.y, MaxY);
MaxY = Maximum(To.y, MaxY);
if (To.x == From.x)
{
auto Increment = From.y < To.y ? 1 : -1;
for (auto Iterator = From.y; Iterator != To.y; Iterator += Increment)
{
Grid.Set({ From.x, Iterator }, 1);
}
}
else if (From.y == To.y)
{
auto Increment = From.x < To.x ? 1 : -1;
for (auto Iterator = From.x; Iterator != To.x; Iterator += Increment)
{
Grid.Set({ Iterator, From.y }, 1);
}
}
}
}
for (auto Iterator = 0; Iterator < 3000; ++Iterator) {
Grid.Set({ Iterator, MaxY + 2 }, 1);
}
//RecordProperty("Grid Filled");
//for (auto& Item : Grid) {
// wxString Line = "";
// for (auto& Char : Item) {
// Line += Char == wxNOT_FOUND ? '.' : '#' ;
// }
// RecordProperty(Line);
//}
wxInt32 SandDrops = 0, TotalSand = 0;
bool SandDropped = false;
for (auto Iterator = 0; !SandDropped; ++Iterator)
{
wxPoint Start(1500, 0);
while (true)
{
Start += wxPoint(0, 1);
if (Start.y >= 1500)
{
SandDrops = Iterator;
RecordProperty(wxString("Off the grid after: ") + Iterator + "Drops of sand");
SandDropped = true;
break;
}
else if (Grid.Value(Start) != wxNOT_FOUND)
{
if (Grid.Value(Start + wxPoint(-1, 0)) == wxNOT_FOUND)
{
Start += wxPoint(-1, 0);
}
else if (Grid.Value(Start + wxPoint(1, 0)) == wxNOT_FOUND)
{
Start += wxPoint(1, 0);
}
else
{
Grid.Set(Start + wxPoint(0, -1), 0); // All sand / rock => Block right above
if (Grid.Value({ 1500,0 }) != wxNOT_FOUND)
{
TotalSand = Iterator++;
RecordProperty(wxString("Grid full after: ") + TotalSand + "Drops of sand");
SandDropped = true;
break;
}
break;
}
}
}
}
EXPECT_EQ(888, SandDrops);
EXPECT_EQ(26461, TotalSand);
for (auto Index = 0; Index <= MaxY; ++Index) {
wxString Line = "";
for (auto& Char : Grid[Index])
{
switch (Char)
{
case -1:
{
Line += '_';
break;
}
case 0:
{
Line += '0';
break;
}
case 1:
{
Line += '#';
break;
}
}
}
RecordProperty(Line);
}
}