generated from sigonasr2/CPlusPlusProjectTemplate
In progress?
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
6552fb154b
commit
dc6f9e07c7
Binary file not shown.
116
main.cpp
116
main.cpp
@ -1,14 +1,95 @@
|
||||
#define OLC_PGE_APPLICATION
|
||||
#include "pixelGameEngine.h"
|
||||
#include "olcutils.h"
|
||||
#include "BigInt.h"
|
||||
#include "InfInt.h"
|
||||
|
||||
using namespace olc;
|
||||
// C++ program to implement
|
||||
// the above approach
|
||||
|
||||
|
||||
//Break down each number into its raw factors.
|
||||
//The number itself is also stored.
|
||||
//If the factorization is larger than 19, we can discard it. It shouldn't matter.
|
||||
|
||||
std::vector<int>factor(int numb){
|
||||
std::vector<int>factors;
|
||||
while (numb>1){
|
||||
for (int i=2;i<numb;i++){
|
||||
if (numb%i==0){
|
||||
factors.push_back(i);
|
||||
numb/=i;
|
||||
goto next;
|
||||
}
|
||||
}
|
||||
factors.push_back(numb);
|
||||
numb/=numb;
|
||||
next:;
|
||||
}
|
||||
factors.push_back(1);
|
||||
return factors;
|
||||
}
|
||||
|
||||
struct factorNumber{
|
||||
void SetupOffsets(){
|
||||
offsets.push_back({23,0});
|
||||
offsets.push_back({19,0});
|
||||
offsets.push_back({13,0});
|
||||
offsets.push_back({17,0});
|
||||
}
|
||||
std::vector<int>factors;
|
||||
std::vector<std::pair<int,int>>offsets;
|
||||
factorNumber(int numb){
|
||||
SetupOffsets();
|
||||
}
|
||||
factorNumber&operator=(int numb){
|
||||
factors.clear();
|
||||
offsets.clear();
|
||||
SetupOffsets();
|
||||
factors=factor(numb);
|
||||
return*this;
|
||||
}
|
||||
factorNumber&operator+=(int numb){
|
||||
for (int i=0;i<offsets.size();i++){
|
||||
std::pair<int,int>&offset=offsets[i];
|
||||
offset.second=(offset.second+numb)%offset.first;
|
||||
}
|
||||
return*this;
|
||||
}
|
||||
factorNumber&operator*=(int numb){
|
||||
std::vector<int>factors=factor(numb);
|
||||
for (int i=0;i<factors.size();i++){
|
||||
for (int j=0;j<this->factors.size();j++){
|
||||
if (this->factors[j]==factors[i]){
|
||||
goto gonext;
|
||||
}
|
||||
}
|
||||
this->factors.push_back(factors[i]);
|
||||
gonext:;
|
||||
}
|
||||
return*this;
|
||||
}
|
||||
int operator%(int numb){
|
||||
for (int i=0;i<offsets.size();i++){
|
||||
if (offsets[i].first==numb){
|
||||
if (offsets[i].second==0) {
|
||||
for (int j=0;j<factors.size();j++){
|
||||
if (factors[j]==numb){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//std::cout<<"Not divisible!"<<std::endl;
|
||||
return -1;
|
||||
} else {
|
||||
return offsets[i].second;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout<<"No divisibility information for modulus "<<numb<<" !! THIS SHOULDN'T BE HAPPENING !!"<<std::endl;
|
||||
return -1; //This shouldn't happen??
|
||||
}
|
||||
};
|
||||
|
||||
struct Monkey{
|
||||
std::vector<BigInt> items;
|
||||
std::vector<factorNumber> items;
|
||||
char operation;
|
||||
int operation_amt;
|
||||
int divisible;
|
||||
@ -22,22 +103,26 @@ int main()
|
||||
std::vector<Monkey> monkeys;
|
||||
std::ifstream file("input");
|
||||
|
||||
monkeys.push_back({{89,95,92,64,87,68},'*',11,2,7,4});
|
||||
/*monkeys.push_back({{89,95,92,64,87,68},'*',11,2,7,4});
|
||||
monkeys.push_back({{87,67},'+',1,13,3,6});
|
||||
monkeys.push_back({{95, 79, 92, 82, 60},'+',6,3,1,6});
|
||||
monkeys.push_back({{67, 97, 56},'*',-1,17,7,0});
|
||||
monkeys.push_back({{80, 68, 87, 94, 61, 59, 50, 68},'*',7,19,5,2});
|
||||
monkeys.push_back({{73, 51, 76, 59},'+',8,7,2,1});
|
||||
monkeys.push_back({{92},'+',5,11,3,0});
|
||||
monkeys.push_back({{99, 76, 78, 76, 79, 90, 89},'+',7,5,4,5});
|
||||
monkeys.push_back({{99, 76, 78, 76, 79, 90, 89},'+',7,5,4,5});*/
|
||||
monkeys.push_back({{79,98},'*',19,23,2,3});
|
||||
monkeys.push_back({{54, 65, 75, 74},'+',6,19,2,0});
|
||||
monkeys.push_back({{79, 60, 97},'*',-1,13,1,3});
|
||||
monkeys.push_back({{74},'+',3,17,0,1});
|
||||
|
||||
for (int round=0;round<10000;round++){
|
||||
for (int i=0;i<monkeys.size();i++){
|
||||
Monkey&m=monkeys[i];
|
||||
m.inspAmt+=m.items.size();
|
||||
for (int j=0;j<m.items.size();j++){
|
||||
BigInt worryLevel=m.items[j];
|
||||
std::cout<<worryLevel<<std::endl;
|
||||
factorNumber worryLevel=m.items[j];
|
||||
//std::cout<<worryLevel<<std::endl;
|
||||
if (m.operation=='+'){
|
||||
if (m.operation_amt!=-1){
|
||||
worryLevel+=m.operation_amt;
|
||||
@ -47,13 +132,9 @@ int main()
|
||||
} else {
|
||||
if (m.operation_amt!=-1){
|
||||
worryLevel*=m.operation_amt;
|
||||
} else {
|
||||
worryLevel*=worryLevel;
|
||||
}
|
||||
}
|
||||
//worryLevel/=3;
|
||||
BigInt zero{"0"};
|
||||
if (worryLevel%m.divisible==zero){
|
||||
if (worryLevel%m.divisible==0){
|
||||
Monkey&newMonkey=monkeys[m.trueThrow];
|
||||
newMonkey.items.push_back(worryLevel);
|
||||
m.items.erase(m.items.begin());
|
||||
@ -66,6 +147,13 @@ int main()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (round==19||round==0||(round+1)%1000==0){
|
||||
std::cout<<"Round "<<(round+1)<<"..."<<std::endl;
|
||||
for (int i=0;i<monkeys.size();i++){
|
||||
Monkey&m=monkeys[i];
|
||||
std::cout<<" Monkey "<<i<<": "<<m.inspAmt<<" inspected"<<std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(monkeys.begin(),monkeys.end(),[](Monkey&first,Monkey&second){return second.inspAmt<first.inspAmt;});
|
||||
@ -74,7 +162,7 @@ int main()
|
||||
std::cout<<"Monkey "<<i<<": "<<m.inspAmt<<" inspections"<<std::endl;
|
||||
}
|
||||
|
||||
std::cout<<(monkeys[0].inspAmt*monkeys[1].inspAmt)<<std::endl;
|
||||
std::cout<<((long)monkeys[0].inspAmt*(long)monkeys[1].inspAmt)<<std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
27
testInput
Normal file
27
testInput
Normal file
@ -0,0 +1,27 @@
|
||||
Monkey 0:
|
||||
Starting items: 79, 98
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 23
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 54, 65, 75, 74
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 79, 60, 97
|
||||
Operation: new = old * old
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 74
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 1
|
Loading…
x
Reference in New Issue
Block a user