Finish Day5Part1; start Part 2
This commit is contained in:
parent
d6d77b269f
commit
cbc075544c
6 changed files with 195 additions and 0 deletions
10
day5/data/testInput.txt
Normal file
10
day5/data/testInput.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
0,9 -> 5,9
|
||||
8,0 -> 0,8
|
||||
9,4 -> 3,4
|
||||
2,2 -> 2,1
|
||||
7,0 -> 7,4
|
||||
6,4 -> 2,0
|
||||
0,9 -> 2,9
|
||||
3,4 -> 1,4
|
||||
0,0 -> 8,8
|
||||
5,5 -> 8,2
|
BIN
day5/day5Part1
Executable file
BIN
day5/day5Part1
Executable file
Binary file not shown.
61
day5/day5Part1.cpp
Normal file
61
day5/day5Part1.cpp
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int arrayDimension = 1000;
|
||||
int inputLineCount = 500;
|
||||
int main (void)
|
||||
{
|
||||
int map[arrayDimension][arrayDimension] = {};
|
||||
string rawInput;
|
||||
int x1[inputLineCount] = {};
|
||||
int x2[inputLineCount] = {};
|
||||
int y1[inputLineCount] = {};
|
||||
int y2[inputLineCount] = {};
|
||||
string comma = ",";
|
||||
string arrow = " -> ";
|
||||
int lineNumber = 0;
|
||||
int overlapCount = 0;
|
||||
ifstream readFile ("data/input.txt", ios::in);
|
||||
|
||||
//Read file into arrays
|
||||
while(getline(readFile, rawInput))
|
||||
{
|
||||
size_t x1End = rawInput.find(comma);
|
||||
x1[lineNumber] = stoi(rawInput.substr(0,x1End));
|
||||
size_t y1End = rawInput.find(arrow);
|
||||
y1[lineNumber] = stoi(rawInput.substr(x1End+1,y1End));
|
||||
size_t x2End = rawInput.find(comma, y1End);
|
||||
x2[lineNumber] = stoi(rawInput.substr(y1End+4,x2End));
|
||||
size_t y2End = rawInput.length()-1;
|
||||
y2[lineNumber] = stoi(rawInput.substr(x2End+1,y2End));
|
||||
lineNumber++;
|
||||
}
|
||||
|
||||
for(int i=0;i<inputLineCount;i++)
|
||||
{
|
||||
if(x1[i]==x2[i])
|
||||
{
|
||||
if(y1[i] < y2[i]) for(int j=y1[i];j<=y2[i];j++) map[j][x1[i]]++;
|
||||
else if(y2[i] < y1[i]) for(int j=y2[i];j<=y1[i];j++) map[j][x1[i]]++;
|
||||
}
|
||||
if(y1[i]==y2[i])
|
||||
{
|
||||
if(x1[i] < x2[i]) for(int j=x1[i];j<=x2[i];j++) map[y1[i]][j]++;
|
||||
else if(x2[i] < x1[i]) for(int j=x2[i];j<=x1[i];j++) map[y1[i]][j]++;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0;i<arrayDimension;i++)
|
||||
{
|
||||
for(int j=0;j<arrayDimension;j++)
|
||||
{
|
||||
if(map[i][j]>1)overlapCount++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << overlapCount << endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
day5/day5Part2
Executable file
BIN
day5/day5Part2
Executable file
Binary file not shown.
101
day5/day5Part2.cpp
Normal file
101
day5/day5Part2.cpp
Normal file
|
@ -0,0 +1,101 @@
|
|||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
int arrayDimension = 10;//1000;
|
||||
int inputLineCount = 10;//500;
|
||||
int main (void)
|
||||
{
|
||||
int map[arrayDimension][arrayDimension] = {};
|
||||
string rawInput;
|
||||
int x1[inputLineCount] = {};
|
||||
int x2[inputLineCount] = {};
|
||||
int y1[inputLineCount] = {};
|
||||
int y2[inputLineCount] = {};
|
||||
string comma = ",";
|
||||
string arrow = " -> ";
|
||||
int lineNumber = 0;
|
||||
int overlapCount = 0;
|
||||
ifstream readFile ("data/testInput.txt", ios::in);
|
||||
|
||||
//Read file into arrays
|
||||
while(getline(readFile, rawInput))
|
||||
{
|
||||
size_t x1End = rawInput.find(comma);
|
||||
x1[lineNumber] = stoi(rawInput.substr(0,x1End));
|
||||
size_t y1End = rawInput.find(arrow);
|
||||
y1[lineNumber] = stoi(rawInput.substr(x1End+1,y1End));
|
||||
size_t x2End = rawInput.find(comma, y1End);
|
||||
x2[lineNumber] = stoi(rawInput.substr(y1End+4,x2End));
|
||||
size_t y2End = rawInput.length()-1;
|
||||
y2[lineNumber] = stoi(rawInput.substr(x2End+1,y2End));
|
||||
lineNumber++;
|
||||
}
|
||||
|
||||
for(int index=0;index<inputLineCount;index++)
|
||||
{
|
||||
if(x1[index]==x2[index])
|
||||
{
|
||||
if(y1[index] < y2[index])
|
||||
{
|
||||
for(int j=y1[index];j<=y2[index];j++) map[j][x1[index]]++;
|
||||
}
|
||||
else if(y2[index] < y1[index])
|
||||
{
|
||||
for(int j=y2[index];j<=y1[index];j++) map[j][x1[index]]++;
|
||||
}
|
||||
|
||||
}
|
||||
else if(y1[index]==y2[index])
|
||||
{
|
||||
if(x1[index] < x2[index]) for(int j=x1[index];j<=x2[index];j++) map[y1[index]][j]++;
|
||||
else if(x2[index] < x1[index]) for(int j=x2[index];j<=x1[index];j++) map[y1[index]][j]++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(x1[index] < x2[index])
|
||||
{
|
||||
for (int i=x1[index];i<x2[index];i++)
|
||||
{
|
||||
if(y1[index] < y2[index])
|
||||
{
|
||||
for(int j=y1[index];j<=y2[index];j++) map[j][i]++;
|
||||
}
|
||||
else if(y2[index] < y1[index])
|
||||
{
|
||||
for(int j=y2[index];j<=y1[index];j++) map[j][i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(x2[index] < x1[index])
|
||||
{
|
||||
for (int i=x2[index];i<x1[index];i++)
|
||||
{
|
||||
if(y1[index] < y2[index])
|
||||
{
|
||||
for(int j=y1[index];j<=y2[index];j++) map[j][i]++;
|
||||
}
|
||||
else if(y2[index] < y1[index])
|
||||
{
|
||||
for(int j=y2[index];j<=y1[index];j++) map[j][i]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(int i=0;i<arrayDimension;i++)
|
||||
{
|
||||
for(int j=0;j<arrayDimension;j++)
|
||||
{
|
||||
cout << map[j][i];
|
||||
if(map[j][i>1])overlapCount++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
cout << overlapCount << endl;
|
||||
|
||||
return 0;
|
||||
}
|
23
day5/part2Instructions.txt
Normal file
23
day5/part2Instructions.txt
Normal file
|
@ -0,0 +1,23 @@
|
|||
Unfortunately, considering only horizontal and vertical lines doesn't give you the full picture; you need to also consider diagonal lines.
|
||||
|
||||
Because of the limits of the hydrothermal vent mapping system, the lines in your list will only ever be horizontal, vertical, or a diagonal line at exactly 45 degrees. In other words:
|
||||
|
||||
An entry like 1,1 -> 3,3 covers points 1,1, 2,2, and 3,3.
|
||||
An entry like 9,7 -> 7,9 covers points 9,7, 8,8, and 7,9.
|
||||
|
||||
Considering all lines from the above example would now produce the following diagram:
|
||||
|
||||
1.1....11.
|
||||
.111...2..
|
||||
..2.1.111.
|
||||
...1.2.2..
|
||||
.112313211
|
||||
...1.2....
|
||||
..1...1...
|
||||
.1.....1..
|
||||
1.......1.
|
||||
222111....
|
||||
|
||||
You still need to determine the number of points where at least two lines overlap. In the above example, this is still anywhere in the diagram with a 2 or larger - now a total of 12 points.
|
||||
|
||||
Consider all of the lines. At how many points do at least two lines overlap?
|
Loading…
Add table
Add a link
Reference in a new issue