From e9487c662cf37afe94a41608c39879d744dcc33e Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Sun, 11 Dec 2022 15:10:04 +0100 Subject: [PATCH] Day11 --- src/days/11/Day11.cpp | 102 ++++++++++++++++++++++++++++++++++++++++-- src/days/11/Day11.h | 19 ++++++++ src/util.h | 4 ++ 3 files changed, 122 insertions(+), 3 deletions(-) diff --git a/src/days/11/Day11.cpp b/src/days/11/Day11.cpp index 8376f31..571451d 100644 --- a/src/days/11/Day11.cpp +++ b/src/days/11/Day11.cpp @@ -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(); -} \ No newline at end of file + auto monkeys = parseMonkeys(); + + Simulate(monkeys, 10000, 1, GetMaxValue(monkeys)); + MyInt result = GetLevelOfMonkeyBusiness(monkeys); + + return to_string(result); +} + +void Day11::Simulate(vector &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 &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 &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::parseMonkeys() { + vector 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; +} diff --git a/src/days/11/Day11.h b/src/days/11/Day11.h index 29ebeba..be2efba 100644 --- a/src/days/11/Day11.h +++ b/src/days/11/Day11.h @@ -1,8 +1,27 @@ #pragma once #include "../../Day.h" +#include +#include + +typedef uint64 MyInt; class Day11 : public Day { + + struct Monkey { + list itemStack; + std::function operation; + MyInt testDivisor; + pair nextMonkeys; + size_t inspections = 0; + }; + + vector parseMonkeys(); + + static MyInt GetMaxValue(const vector&); + static MyInt GetLevelOfMonkeyBusiness(const vector&); + static void Simulate(vector&, MyInt, MyInt, MyInt); + protected: Result Task1() override; diff --git a/src/util.h b/src/util.h index 35a400d..f3d2df7 100644 --- a/src/util.h +++ b/src/util.h @@ -7,10 +7,14 @@ #include #include #include +#include + +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 findAll(const string& data, const string& toSearch){ vector indices;