end day9part1, start day9part2
This commit is contained in:
parent
51c835b883
commit
dc7bd4d2a3
5 changed files with 349 additions and 0 deletions
5
day9/data/testInput.txt
Normal file
5
day9/data/testInput.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
BIN
day9/day9Part1
Executable file
BIN
day9/day9Part1
Executable file
Binary file not shown.
155
day9/day9Part1.cpp
Normal file
155
day9/day9Part1.cpp
Normal file
|
@ -0,0 +1,155 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int lineSize = 100; //100 for input
|
||||
string rawInput;
|
||||
string tempString;
|
||||
vector<int> heightMap;
|
||||
int index = 0;
|
||||
vector<int> lowPointIndexes;
|
||||
int totalRisk = 0;
|
||||
ifstream readFile ("data/input.txt", ios::in);
|
||||
|
||||
// Pull file into RAM
|
||||
while(getline(readFile, rawInput))
|
||||
{
|
||||
for (int i = 0; i < rawInput.size(); i++)
|
||||
{
|
||||
heightMap.push_back((rawInput[i] - 48));
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate through map
|
||||
for (index = 0; index < heightMap.size(); index++)
|
||||
{
|
||||
if (index == 0)
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index < lineSize)
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if ((index % lineSize) == (lineSize - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index == (lineSize - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if ((index % lineSize) == 0)
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index == (heightMap.size() - lineSize - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if ((index / lineSize) == ((heightMap.size() - 1) / lineSize))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index == (heightMap.size() - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << endl << "Low Indexes: ";
|
||||
for (int i = 0; i < lowPointIndexes.size(); i++)
|
||||
{
|
||||
cout << lowPointIndexes.at(i) << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
// All low points are now saved in lowPointIndexes as indicies
|
||||
// --------
|
||||
// Calculate Risk
|
||||
|
||||
int counter = 0;
|
||||
for (index = 0; index < heightMap.size(); index++)
|
||||
{
|
||||
for (counter = 0; counter < lowPointIndexes.size(); counter++)
|
||||
{
|
||||
if (index == lowPointIndexes.at(counter))
|
||||
{
|
||||
totalRisk += 1 + heightMap[index];
|
||||
cout << heightMap[index] << ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << "Total Risk: " << totalRisk;
|
||||
return 0;
|
||||
}
|
145
day9/day9Part2.cpp
Normal file
145
day9/day9Part2.cpp
Normal file
|
@ -0,0 +1,145 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int lineSize = 100; //100 for input
|
||||
string rawInput;
|
||||
string tempString;
|
||||
vector<int> heightMap;
|
||||
vector<bool> basinBarrier;
|
||||
int index = 0;
|
||||
vector<int> lowPointIndexes;
|
||||
int totalRisk = 0;
|
||||
ifstream readFile ("data/input.txt", ios::in);
|
||||
|
||||
// Pull file into RAM
|
||||
while(getline(readFile, rawInput))
|
||||
{
|
||||
for (int i = 0; i < rawInput.size(); i++)
|
||||
{
|
||||
heightMap.push_back((rawInput[i] - 48));
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate through map
|
||||
for (index = 0; index < heightMap.size(); index++)
|
||||
{
|
||||
if (index == 0)
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index < lineSize)
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if ((index % lineSize) == (lineSize - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index == (lineSize - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if ((index % lineSize) == 0)
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index == (heightMap.size() - lineSize - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if ((index / lineSize) == ((heightMap.size() - 1) / lineSize))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else if (index == (heightMap.size() - 1))
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (
|
||||
heightMap.at(index) < heightMap.at(index + 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - 1) &&
|
||||
heightMap.at(index) < heightMap.at(index - lineSize) &&
|
||||
heightMap.at(index) < heightMap.at(index + lineSize)
|
||||
)
|
||||
{
|
||||
lowPointIndexes.push_back(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// All low points are now saved in lowPointIndexes as indicies
|
||||
// --------
|
||||
|
||||
basinBarrier.reserve(heightMap.size());
|
||||
for (index = 0; index < heightMap.size();index++)
|
||||
{
|
||||
if (heightMap.at(index) == 9)
|
||||
{
|
||||
basinBarrier.at(index) = true;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
44
day9/part2Instructions.txt
Normal file
44
day9/part2Instructions.txt
Normal file
|
@ -0,0 +1,44 @@
|
|||
--- Part Two ---
|
||||
|
||||
Next, you need to find the largest basins so you know what areas are most important to avoid.
|
||||
|
||||
A basin is all locations that eventually flow downward to a single low point. Therefore, every low point has a basin, although some basins are very small. Locations of height 9 do not count as being in any basin, and all other locations will always be part of exactly one basin.
|
||||
|
||||
The size of a basin is the number of locations within the basin, including the low point. The example above has four basins.
|
||||
|
||||
The top-left basin, size 3:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
The top-right basin, size 9:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
The middle basin, size 14:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
The bottom-right basin, size 9:
|
||||
|
||||
2199943210
|
||||
3987894921
|
||||
9856789892
|
||||
8767896789
|
||||
9899965678
|
||||
|
||||
Find the three largest basins and multiply their sizes together. In the above example, this is 9 * 14 * 9 = 1134.
|
||||
|
||||
What do you get if you multiply together the sizes of the three largest basins?
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue