-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbipartite_instance.cpp
More file actions
218 lines (162 loc) · 7.21 KB
/
bipartite_instance.cpp
File metadata and controls
218 lines (162 loc) · 7.21 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
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
// Logicwise
// Copyright (c) 2026 Frog Singing (@frog-singing)
// SPDX-License-Identifier: MIT
#include <logicwise.h>
#include <array> // std::array
#include <vector> // std::vector
#include <ranges> // std::ranges
#include <algorithm> // std::max
#include <iostream> // std::cout, std::boolalpha, std::endl
// If this file compiles successfully, then all logical assertions have passed.
namespace logicwise::test
{
struct combat_attribute
{
int attack{};
int defense{};
int health{};
};
//a >= 0, b > 0
static constexpr int ceiling_division(int a, int b) noexcept { return (a + b - 1) / b; }
static constexpr auto adventurer_beats_monsters = [] (auto&& adventurer, auto&& monsters)
{
int adventurer_health{ adventurer.health };
if (adventurer_health <= 0) { return false; }
int monster_quantity{ static_cast<int>(std::ranges::size(monsters)) };
int defeated_monster_count{ 0 };
while (true)
{
if (defeated_monster_count == monster_quantity) { return true; }
int current_monster_health{ monsters[defeated_monster_count].health };
while (current_monster_health > 0)
{
current_monster_health -= (std::max)(1, (adventurer.attack - monsters[defeated_monster_count].defense));
for (int i = defeated_monster_count; i < monster_quantity; ++i)
{
adventurer_health -= (std::max)(1, (monsters[i].attack - adventurer.defense));
}
if (adventurer_health <= 0) { return false; }
}
++defeated_monster_count;
}
};
static constexpr auto adventurer_beats_adventurer = [] (auto&& adventurer1, auto&& adventurer2)
{
int adventurer_1_strike_count = ceiling_division(
adventurer2.health,
(std::max)(1, (adventurer1.attack - adventurer2.defense))
);
int adventurer_2_strike_count = ceiling_division(
adventurer1.health,
(std::max)(1, (adventurer2.attack - adventurer1.defense))
);
return adventurer_1_strike_count < adventurer_2_strike_count;
};
}
int main()
{
using namespace logicwise;
using namespace logicwise::quantifier;
using namespace logicwise::arrangement; // check this out in logicwise/arrangement.h
using namespace logicwise::test;
// compile-time vector-like container --------------------------------------------------------------------------------
static constexpr combat_attribute
warrior { .attack = 14, .defense = 10, .health = 108 },
mage { .attack = 19, .defense = 8, .health = 75 },
rogue { .attack = 23, .defense = 5, .health = 72 };
static constexpr std::array adventurer_template_array{ warrior, mage, rogue };
static constexpr combat_attribute
slime { .attack = 12, .defense = 8, .health = 30 },
zombie { .attack = 14, .defense = 4, .health = 40 },
scorpion { .attack = 16, .defense = 9, .health = 22 },
wolf { .attack = 18, .defense = 6, .health = 26 };
static constexpr std::array monster_template_array{ slime, zombie, scorpion, wolf };
static_assert(
rangewise<all_of, cartesian_pair>
::between(adventurer_template_array, monster_template_array)
.satisfies([] (auto&& adventurer, auto&& monster) {
if (adventurer.attack <= monster.defense || monster.attack <= adventurer.defense) { return false; }
int adventurer_strike_count = ceiling_division(monster.health, (adventurer.attack - monster.defense));
if (adventurer_strike_count <= 1 || adventurer_strike_count > 5) { return false; }
int monster_strike_count = ceiling_division(adventurer.health, (monster.attack - adventurer.defense));
if (monster_strike_count <= 3 || monster_strike_count > 100) { return false; }
return monster_strike_count - adventurer_strike_count >= 2;
}),
"compile-time container: adventurer-monster balance"
);
static_assert(
rangewise<all_of, zip_pair_truncation>
::between(adventurer_template_array, adventurer_template_array)
.satisfies([] (auto&& adventurer, auto&& same_class_adventurer) {
if (adventurer.attack <= same_class_adventurer.defense) { return false; }
int strike_count = ceiling_division(
same_class_adventurer.health,
(adventurer.attack - same_class_adventurer.defense)
);
if (strike_count <= 3 || strike_count > 30) { return false; }
return true;
}),
"compile-time container: same class adventurer balance"
);
static_assert(
rangewise<all_of, circular_adjacent_pair>
::in(adventurer_template_array)
.satisfies([] (auto&& adventurer1, auto&& adventurer2) {
return adventurer_beats_adventurer(adventurer2, adventurer1);
}),
"compile-time container: different class adventurer balance"
);
// compile-time vector-like container and runtime vector-like container --------------------------------------------------------------------------------
std::cout << std::boolalpha;
static constexpr std::array<combat_attribute, 2>
monster_pair_1 { slime, slime },
monster_pair_2 { zombie, zombie },
monster_pair_3 { scorpion, scorpion },
monster_pair_4 { wolf, wolf };
static constexpr std::array dungeon_monster_pairs{
monster_pair_1,
monster_pair_2,
monster_pair_3
};
std::vector<combat_attribute> adventurer_party_A{};
adventurer_party_A.push_back(warrior);
adventurer_party_A.push_back(mage);
adventurer_party_A.push_back(rogue);
adventurer_party_A.push_back(warrior);
adventurer_party_A.push_back(mage);
bool adventurer_party_A_clears_dungeon_X =
rangewise<all_of, zip_pair_truncation>
::between(dungeon_monster_pairs, adventurer_party_A) // testing complie time container and runtime container
.satisfies([] (auto&& monsters, auto&& adventurer) { return adventurer_beats_monsters(adventurer, monsters); });
std::cout << "adventurer party A clears dungeon X: " << adventurer_party_A_clears_dungeon_X << std::endl;
// combat_attribute{} is a placeholder and won't be used for padding in this scenario,
// because adventurer_party_A is longer
bool adventurer_party_A_clears_dungeon_Y =
rangewise<all_of, zip_pair_padding>
::with_padding(combat_attribute{}, monster_pair_4)
.between(adventurer_party_A, dungeon_monster_pairs) // testing runtime container and complie time container
.satisfies(adventurer_beats_monsters);
std::cout << "adventurer party A clears dungeon Y: " << adventurer_party_A_clears_dungeon_Y << std::endl;
// runtime vector-like container --------------------------------------------------------------------------------
std::cout << std::endl;
std::vector<combat_attribute> adventurer_party_B{ rogue, rogue, rogue };
for (auto& adventurer : adventurer_party_B)
{
adventurer = warrior;
}
bool adventurer_party_A_wins_once_in_the_first_battle =
rangewise<exactly<1>, zip_pair_truncation>
::between(adventurer_party_A, adventurer_party_B)
.satisfies(adventurer_beats_adventurer);
std::cout << "adventurer party A wins once in the first battle: "
<< adventurer_party_A_wins_once_in_the_first_battle << std::endl;
// only warrior will be used for padding since adventurer_party_B is shorter
bool adventurer_party_A_wins_twice_in_the_second_battle =
rangewise<exactly<2>, zip_pair_padding>
::with_padding(rogue, warrior)
.between(adventurer_party_A, adventurer_party_B)
.satisfies(adventurer_beats_adventurer);
std::cout << "adventurer party A wins twice in the second battle: "
<< adventurer_party_A_wins_twice_in_the_second_battle << std::endl;
std::cout << std::endl << "Hello Logic!" << std::endl;
}