Compare commits

..

No commits in common. 'a263a89db3032879ffb0556a5aad159e8b8cf9e8' and '44c0a9d2067a5aa2917b3105a122b96817fde235' have entirely different histories.

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

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

@ -1,105 +1,9 @@
#include "Day11.h" #include "Day11.h"
Result Day11::Task1() { Result Day11::Task1() {
auto monkeys = parseMonkeys(); return Day::Task1();
Simulate(monkeys, 20, 3, UINT64_MAX);
MyInt result = GetLevelOfMonkeyBusiness(monkeys);
return to_string(result);
} }
Result Day11::Task2() { Result Day11::Task2() {
auto monkeys = parseMonkeys(); return Day::Task2();
}
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,27 +1,8 @@
#pragma once #pragma once
#include "../../Day.h" #include "../../Day.h"
#include <functional>
#include <numeric>
typedef uint64 MyInt;
class Day11 : public Day { 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: protected:
Result Task1() override; Result Task1() override;

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

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

Loading…
Cancel
Save