Compare commits
No commits in common. 'a263a89db3032879ffb0556a5aad159e8b8cf9e8' and '44c0a9d2067a5aa2917b3105a122b96817fde235' have entirely different histories.
a263a89db3
...
44c0a9d206
6 changed files with 22 additions and 141 deletions
@ -1,105 +1,9 @@ |
||||
#include "Day11.h" |
||||
|
||||
Result Day11::Task1() { |
||||
auto monkeys = parseMonkeys(); |
||||
|
||||
Simulate(monkeys, 20, 3, UINT64_MAX); |
||||
MyInt result = GetLevelOfMonkeyBusiness(monkeys); |
||||
|
||||
return to_string(result); |
||||
return Day::Task1(); |
||||
} |
||||
|
||||
Result Day11::Task2() { |
||||
auto monkeys = parseMonkeys(); |
||||
|
||||
Simulate(monkeys, 10000, 1, GetMaxValue(monkeys)); |
||||
MyInt result = GetLevelOfMonkeyBusiness(monkeys); |
||||
|
||||
return to_string(result); |
||||
} |
||||
|
||||
void Day11::Simulate(vector<Monkey> &monkeys, MyInt rounds, MyInt div, MyInt mod) { |
||||
for (int round = 0; round < rounds; round++){ |
||||
for (Monkey &monkey : monkeys){ |
||||
while (!monkey.itemStack.empty()){ |
||||
MyInt worry = monkey.itemStack.front(); |
||||
monkey.itemStack.pop_front(); |
||||
monkey.inspections++; |
||||
|
||||
worry = monkey.operation(worry) / div % mod; |
||||
|
||||
size_t next = (worry % monkey.testDivisor == 0) ? monkey.nextMonkeys.first : monkey.nextMonkeys.second; |
||||
monkeys[next].itemStack.push_back(worry); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
MyInt Day11::GetMaxValue(const vector<Monkey> &monkeys) { |
||||
// Divisors are all prime, so I can modulo the result everytime with the primes product
|
||||
MyInt maxValue = 1; |
||||
for (const Monkey &monkey : monkeys) |
||||
maxValue *= monkey.testDivisor; |
||||
return maxValue; |
||||
} |
||||
|
||||
MyInt Day11::GetLevelOfMonkeyBusiness(const vector<Monkey> &monkeys) { |
||||
MyInt max1 = 0, max2 = 0; |
||||
|
||||
for (const Monkey &monkey : monkeys){ |
||||
if (monkey.inspections > max1){ |
||||
max2 = max1; |
||||
max1 = monkey.inspections; |
||||
} else if (monkey.inspections > max2){ |
||||
max2 = monkey.inspections; |
||||
} |
||||
} |
||||
return max1 * max2; |
||||
} |
||||
|
||||
vector<Day11::Monkey> Day11::parseMonkeys() { |
||||
vector<Monkey> monkeys; |
||||
|
||||
for (size_t i = 0; i < input.size(); i += 7){ |
||||
Monkey m; |
||||
|
||||
// itemStack
|
||||
string listStr = input[i + 1].substr(18); |
||||
for (int sIdx = 0; sIdx < listStr.size(); sIdx += 4) |
||||
m.itemStack.push_back(stoi(listStr.substr(sIdx, 2))); |
||||
|
||||
// operation
|
||||
string opStr = input[i + 2].substr(19); |
||||
size_t opIndex = opStr.find('+'); |
||||
if (opIndex == string::npos) |
||||
opIndex = opStr.find('*'); |
||||
string lhsStr = opStr.substr(0, opIndex - 1); |
||||
string rhsStr = opStr.substr(opIndex + 2); |
||||
char opChar = opStr[opIndex]; |
||||
m.operation = [lhsStr, rhsStr, opChar](MyInt old) -> MyInt { |
||||
MyInt lhs, rhs; |
||||
|
||||
if (lhsStr == "old") lhs = old; |
||||
else lhs = stoi(lhsStr); |
||||
|
||||
if (rhsStr == "old") rhs = old; |
||||
else rhs = stoi(rhsStr); |
||||
|
||||
if (opChar == '+') |
||||
return lhs + rhs; |
||||
else |
||||
return lhs * rhs; |
||||
}; |
||||
|
||||
// testDivisor;
|
||||
m.testDivisor = stoi(input[i + 3].substr(21)); |
||||
|
||||
// nextMonkeys;
|
||||
m.nextMonkeys.first = stoi(input[i + 4].substr(29)); |
||||
m.nextMonkeys.second = stoi(input[i + 5].substr(30)); |
||||
|
||||
monkeys.push_back(m); |
||||
} |
||||
|
||||
return monkeys; |
||||
return Day::Task2(); |
||||
} |
Loading…
Reference in new issue