Memory-optimized lanternfish
This commit is contained in:
parent
15d2a47735
commit
a6416e3877
2 changed files with 35 additions and 22 deletions
BIN
day6/day6Part2
BIN
day6/day6Part2
Binary file not shown.
|
@ -7,9 +7,10 @@ using namespace std;
|
||||||
int main (void)
|
int main (void)
|
||||||
{
|
{
|
||||||
string rawInput;
|
string rawInput;
|
||||||
vector<short int> fishCount = {};
|
long int fishCount[9] = {};
|
||||||
vector<short int> nextFishCount;
|
long int nextFishCount[9] = {};
|
||||||
short int dayCount = 0;
|
int dayCount = 0;
|
||||||
|
long int finalFishCount = 0;
|
||||||
ifstream readFile ("data/input.txt", ios::in);
|
ifstream readFile ("data/input.txt", ios::in);
|
||||||
|
|
||||||
//Read file into arrays
|
//Read file into arrays
|
||||||
|
@ -20,28 +21,28 @@ int main (void)
|
||||||
switch(rawInput[i])
|
switch(rawInput[i])
|
||||||
{
|
{
|
||||||
case '1':
|
case '1':
|
||||||
fishCount.push_back(1);
|
fishCount[1]++;
|
||||||
break;
|
break;
|
||||||
case '2':
|
case '2':
|
||||||
fishCount.push_back(2);
|
fishCount[2]++;
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '3':
|
||||||
fishCount.push_back(3);
|
fishCount[3]++;
|
||||||
break;
|
break;
|
||||||
case '4':
|
case '4':
|
||||||
fishCount.push_back(4);
|
fishCount[4]++;
|
||||||
break;
|
break;
|
||||||
case '5':
|
case '5':
|
||||||
fishCount.push_back(5);
|
fishCount[5]++;
|
||||||
break;
|
break;
|
||||||
case '6':
|
case '6':
|
||||||
fishCount.push_back(6);
|
fishCount[6]++;
|
||||||
break;
|
break;
|
||||||
case '7':
|
case '7':
|
||||||
fishCount.push_back(7);
|
fishCount[7]++;
|
||||||
break;
|
break;
|
||||||
case '8':
|
case '8':
|
||||||
fishCount.push_back(8);
|
fishCount[8]++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
continue;
|
continue;
|
||||||
|
@ -49,21 +50,33 @@ int main (void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nextFishCount = fishCount;
|
cout << "Initial Size: ";
|
||||||
|
for(int i=0;i<9;i++)
|
||||||
|
{
|
||||||
|
cout << fishCount[i] << ",";
|
||||||
|
}
|
||||||
|
|
||||||
for(dayCount;dayCount<256;dayCount++)
|
for(dayCount;dayCount<256;dayCount++)
|
||||||
{
|
{
|
||||||
for(int i=0;i<fishCount.size();i++)
|
//cout << "New Day! Processing day " << dayCount << "..." << endl;
|
||||||
|
for(int i=0;i<8;i++)
|
||||||
{
|
{
|
||||||
if(fishCount[i] == 0)
|
nextFishCount[i] = fishCount[i+1];
|
||||||
{
|
}
|
||||||
nextFishCount[i] = 6;
|
nextFishCount[8] = fishCount[0];
|
||||||
nextFishCount.push_back(8);
|
nextFishCount[6] += fishCount[0];
|
||||||
}
|
for(int i=0;i<9;i++)
|
||||||
else nextFishCount[i]--;
|
{
|
||||||
|
fishCount[i] = nextFishCount[i];
|
||||||
}
|
}
|
||||||
cout << "New Day! Processing day " << dayCount << "..." << endl;
|
|
||||||
fishCount = nextFishCount;
|
|
||||||
}
|
}
|
||||||
cout << "Fish Count: " << fishCount.size() << endl;
|
|
||||||
|
cout << endl << "End Size: ";
|
||||||
|
for(int i=0;i<9;i++)
|
||||||
|
{
|
||||||
|
cout << fishCount[i] << ",";
|
||||||
|
finalFishCount += fishCount[i];
|
||||||
|
}
|
||||||
|
cout << "Fish Count: " << finalFishCount << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue