aboutsummaryrefslogtreecommitdiff
path: root/week02/Exercise4.cpp
blob: c2d6d75e6fffe5da43ce4237c8c359838ade6bb8 (plain) (blame)
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
struct Material {
	int id;
	int startingType;
	int endingType;
};

struct MaterialArray {
private:
	Material* elems;
	int size;
	int last;

public:
	MaterialArray(int size) {
		this->size = size;
		elems = new Material[size];
		last = 0;
	}

	void Append(const Material& m) {
		if (size == last) return;
		elems[last++] = m;
	}

	int GetSize() {
		return size;
	}

	Material* GetByID(int id) {
		for (int i = 0; i < last; i++) {
			if (elems[i].id == id) {
				return &elems[i];
			}
		}
		return nullptr;
	}

	Material* GetByStartingType(int type) {
		for (int i = 0; i < last; i++) {
			if (elems[i].startingType == type) {
				return &elems[i];
			}
		}
		return nullptr;
	}
};

struct Machine {
private:
	struct MaterialEfficiency {
		int id;
		float percentage;
	};
	MaterialEfficiency *efficiency;
	int knownMaterials;

	int availableThroughput;

public:
	int ConvertMaterial(int materialID, int quantity) {
		if (availableThroughput == 0) {
			return 0;
		}

		int converts = quantity * GetEfficiency(materialID);
		availableThroughput -= converts;
		// Губим материал, понеже машината се е счупила по средата на обработка
		if (availableThroughput < 0) {
			converts += availableThroughput;
			availableThroughput = 0;
		}
		return converts;
	}

	int WillConvert(int materialID, int quantity) {
		if (availableThroughput == 0) {
			return 0;
		}

		int converts = quantity * GetEfficiency(materialID);
		int newThroughput = availableThroughput - converts;
		if (newThroughput < 0) {
			converts += newThroughput;
		}
		return converts;
	}

	void FixFor(int quantity) {
		availableThroughput += quantity;
	}

	float GetEfficiency(int id) {
		for (int i = 0; i < knownMaterials; i++) {
			if (efficiency[i].id == id) {
				return efficiency[i].percentage;
			}
		}
		return 0;
	}
};

MaterialArray& materialPath(MaterialArray& materials, int startid, int endid) {
	MaterialArray* path = new MaterialArray(materials.GetSize());

	Material* current = materials.GetByID(startid);
	path->Append(*current);
	while(current->id != endid) {
		current = materials.GetByStartingType(current->endingType);
		path->Append(*current);
	}

	return *path;
}

MachineArray& bestMachinePath(MaterialArray& mpath) {

}

int main() {
}