Repository navigation
Expand file tree
/
Copy pathAssignment13.cpp
More file actions
154 lines (135 loc) · 3.3 KB
/
Copy pathAssignment13.cpp
File metadata and controls
154 lines (135 loc) · 3.3 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
#include "AdventClass.h"
#include "FileUtility.h"
#include "LimitFunctions.h"
#include "StringFunctions.h"
#include "StringHandler.h"
class TItem {
public:
TItem(wxInt32 Value = wxNOT_FOUND)
{
this->Value = Value;
}
wxInt32 Value;
TCVector<TItem*> SubList;
wxInt32 ItemValue(void) const
{
if (Value != wxNOT_FOUND) {
return Value;
}
if (SubList.size() == 0) {
return wxNOT_FOUND;
}
return SubList.front()->ItemValue();
}
wxString Print(void) const {
if ((Value == wxNOT_FOUND)
&& SubList.empty())
{
return "[]";
}
if (Value != wxNOT_FOUND)
{
return IntToString(Value);
}
wxString Cumulative = "[";
for (auto& Item : SubList)
{
Cumulative += Item->Print() + ",";
}
Cumulative.RemoveLast();
return Cumulative + "]";
}
};
bool Compare(const TItem& Left, const TItem& Right)
{
return (Left.ItemValue() < Right.ItemValue());
}
class TestAssignment13 : public TAdventClass
{
public:
TItem Compose(wxString Input) {
TItem List;
TCVector<TItem*> Selected = { &List };
auto First = true;
while (0 < Input.length())
{
if (Input.StartsWith('['))
{
if (!First)
{
auto NewList = Selected.back()->SubList.Add(new TItem());
Selected.push_back(NewList);
}
First = false;
Input = Input.Mid(1);
}
else if (Input.StartsWith(']'))
{
Selected.pop_back();
Input = Input.Mid(1);
}
else if (Input.StartsWith(','))
{
Input = Input.Mid(1);
}
else
{
auto Val = StringToInt<wxInt32>(Split(Input, ',')[0]);
Selected.back()->SubList.Add(new TItem(Val));
Input = Input.Mid(Val >= 10 ? 2 : 1);
}
}
return List;
}
};
TEST_F(TestAssignment13, TestSamples)
{
auto Input1 = wxString("[1,1,3,1,1]");
auto Input2 = wxString("[1,1,2,1,1]");
auto Items = Compose(Input1);
auto Items2 = Compose(Input2);
using TSet = TCVector<TItem>;
using TTestPair = struct {
wxString Left; wxString Right;
};
auto Input = ReadFileLines(RealInput());
TCVector<TTestPair> Pairs;
for (auto Iterator = 0; Iterator < Input.size(); Iterator += 2)
{
Pairs.push_back({ Input[Iterator], Input[Iterator + 1] });
}
auto PairsIndices = 1;
auto SummedValue = 0;
for (auto Item : Pairs)
{
auto Left = Compose(Item.Left);
auto Right = Compose(Item.Right);
RecordProperty("Left: ", Left.Print());
RecordProperty("Right: ", Right.Print());
if (RecordProperty("Compare:", (1 == Compare(Left, Right))))
{
SummedValue += PairsIndices;
}
++PairsIndices;
}
TCVector<TItem> AllItems;
AllItems.push_back(Compose("[[6]]"));
AllItems.push_back(Compose("[[2]]"));
for (auto& Item : Input) {
AllItems.push_back(Compose(Item));
}
std::sort(AllItems.begin(), AllItems.end(), Compare);
auto Index1 = 0;
auto Index2 = 0;
for (auto Iterator = 0U; AllItems.size() > Iterator; ++Iterator)
{
if (AllItems.at(Iterator).Print() == "[[6]]") {
Index1 = Iterator + 1;
}
if (AllItems.at(Iterator).Print() == "[[2]]") {
Index2 = Iterator + 1;
}
}
EXPECT_EQ(5393, RecordProperty("Ordered Indices:", SummedValue));
EXPECT_EQ(26712, RecordProperty("IndexedItems", Index1 * Index2));
}