Compare commits

...

3 Commits

  1. 22
      src/days/07/Day07.cpp
  2. 14
      src/days/07/Day07.h
  3. 102
      src/days/11/Day11.cpp
  4. 19
      src/days/11/Day11.h
  5. 2
      src/main.cpp
  6. 4
      src/util.h

@ -4,7 +4,7 @@ Result Day07::Task1() {
Dir* tree = parseTree();
set<Dir*> dirsBelow = tree->getAllDirsBelowSize(100000);
uint64_t sum = accumulate(dirsBelow.begin(), dirsBelow.end(), 0, [](uint64_t s, Dir* dir){
uint64 sum = accumulate(dirsBelow.begin(), dirsBelow.end(), 0, [](uint64 s, Dir* dir){
return s + dir->getSize();
});
@ -16,14 +16,14 @@ Result Day07::Task1() {
Result Day07::Task2() {
Dir* tree = parseTree();
uint64_t toBeFreed = 30000000 - (70000000 - tree->getSize());
uint64 toBeFreed = 30000000 - (70000000 - tree->getSize());
set<Dir*> dirsAbove = tree->getAllDirsAboveSize(toBeFreed);
set<uint64_t> sizes;
set<uint64> sizes;
std::transform(dirsAbove.begin(), dirsAbove.end(), inserter(sizes, sizes.begin()), [](Dir* dir){
return dir->getSize();
});
uint64_t result = *std::min_element(sizes.begin(), sizes.end());
uint64 result = *std::min_element(sizes.begin(), sizes.end());
delete tree;
@ -45,7 +45,7 @@ Dir* Day07::parseTree() {
cwd->items.insert(new Dir(name, cwd));
} else {
size_t ws = pos->find(' ');
uint64_t size = stoi(pos->substr(0, ws));
uint64 size = stoi(pos->substr(0, ws));
string name = pos->substr(ws + 1);
cwd->items.insert(new File(name, cwd, size));
}
@ -83,8 +83,8 @@ Dir *Dir::findRoot() {
return parent->findRoot();
}
uint64_t Dir::getSize() {
return accumulate(items.begin(), items.end(), 0, [](uint64_t s, Item* item){
uint64 Dir::getSize() {
return accumulate(items.begin(), items.end(), 0, [](uint64 s, Item* item){
return s + item->getSize();
});
}
@ -98,7 +98,7 @@ set<Dir*> Dir::getDirs() {
return dirs;
}
set<Dir*> Dir::getAllDirsBelowSize(uint64_t maxSize) {
set<Dir*> Dir::getAllDirsBelowSize(uint64 maxSize) {
set<Dir*> result;
for (Dir* dir : getDirs()){
@ -111,7 +111,7 @@ set<Dir*> Dir::getAllDirsBelowSize(uint64_t maxSize) {
return result;
}
set<Dir *> Dir::getAllDirsAboveSize(uint64_t minSize) {
set<Dir *> Dir::getAllDirsAboveSize(uint64 minSize) {
set<Dir*> result;
for (Dir* dir : getDirs()){
@ -124,11 +124,11 @@ set<Dir *> Dir::getAllDirsAboveSize(uint64_t minSize) {
return result;
}
uint64_t File::getSize() {
uint64 File::getSize() {
return size;
}
uint64_t Item::getSize() {
uint64 Item::getSize() {
return 0;
}

@ -8,7 +8,7 @@ struct Item {
Item(string name, Dir* parent) : name(name), parent(parent){};
string name;
Dir* parent;
virtual uint64_t getSize();
virtual uint64 getSize();
virtual ~Item() = default;
};
@ -16,17 +16,17 @@ struct Dir : Item {
Dir(string name, Dir* parent) : Item(name, parent) {}
set<Item*> items;
Dir* findRoot();
uint64_t getSize();
uint64 getSize();
set<Dir*> getDirs();
set<Dir*> getAllDirsBelowSize(uint64_t maxSize);
set<Dir*> getAllDirsAboveSize(uint64_t minSize);
set<Dir*> getAllDirsBelowSize(uint64 maxSize);
set<Dir*> getAllDirsAboveSize(uint64 minSize);
~Dir();
};
struct File : Item {
File(string name, Dir* parent, uint64_t size) : Item(name, parent), size(size) {}
uint64_t size;
uint64_t getSize();
File(string name, Dir* parent, uint64 size) : Item(name, parent), size(size) {}
uint64 size;
uint64 getSize();
};
class Day07 : public Day {

@ -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;

@ -64,7 +64,7 @@ void parseArgument(const string& arg, int &dayNum, bool &useTestInput, int &test
}
dayNum = stoi(arg.substr(0, tIndex));
useTestInput = true;
testFetchIndex = stoi(arg.substr(tIndex + 1));
testFetchIndex = tIndex + 1 < arg.size() ? stoi(arg.substr(tIndex + 1)) : 0;
}
int main(int argc, char *argv[]) {

@ -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