master
Benjamin Kraft 2 years ago
parent 538b687854
commit e9487c662c
  1. 102
      src/days/11/Day11.cpp
  2. 19
      src/days/11/Day11.h
  3. 4
      src/util.h

@ -1,9 +1,105 @@
#include "Day11.h"
Result Day11::Task1() {
return Day::Task1();
auto monkeys = parseMonkeys();
Simulate(monkeys, 20, 3, UINT64_MAX);
MyInt result = GetLevelOfMonkeyBusiness(monkeys);
return to_string(result);
}
Result Day11::Task2() {
return Day::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;
}

@ -1,8 +1,27 @@
#pragma once
#include "../../Day.h"
#include <functional>
#include <numeric>
typedef uint64 MyInt;
class Day11 : public Day {
struct Monkey {
list<MyInt> itemStack;
std::function<MyInt(MyInt)> operation;
MyInt testDivisor;
pair<size_t, size_t> nextMonkeys;
size_t inspections = 0;
};
vector<Monkey> parseMonkeys();
static MyInt GetMaxValue(const vector<Monkey>&);
static MyInt GetLevelOfMonkeyBusiness(const vector<Monkey>&);
static void Simulate(vector<Monkey>&, MyInt, MyInt, MyInt);
protected:
Result Task1() override;

@ -7,10 +7,14 @@
#include <iostream>
#include <set>
#include <map>
#include <list>
typedef uint64_t uint64;
using std::stoi, std::to_string;
using std::cout, std::endl;
using std::string, std::vector, std::set, std::pair, std::map;
using std::list;
inline vector<size_t> findAll(const string& data, const string& toSearch){
vector<size_t> indices;

Loading…
Cancel
Save