1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Marcus Penate cb5729154d Day 10 2022-12-21 21:53:45 -05:00
Marcus Penate 74a59fd555 Day 9 2022-12-21 21:53:45 -05:00
Marcus Penate e12db36a20 Day 8 2022-12-21 21:53:45 -05:00
6 changed files with 451 additions and 0 deletions

33
Day10/part1.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string line;
int cycle = 1;
int accumulator = 1;
int answer = 0;
while(getline(cin, line))
{
int next_adden = 0;
if (line.substr(0,4).compare("addx") == 0)
{
next_adden = stoi(line.substr(5));
cycle++;
if ((cycle-20)%40 == 0)
{
answer += cycle*accumulator;
}
accumulator += next_adden;
}
cycle++;
if ((cycle-20)%40 == 0)
{
answer += cycle*accumulator;
}
}
cout << answer << endl;
}

50
Day10/part2.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string line;
int cycle = 1;
int x = 0;
int accumulator = 1;
while(getline(cin, line))
{
if (accumulator >= x-1 && accumulator <= x+1)
{
cout << "#";
}
else
{
cout << " ";
}
cycle++;
x++;
if (0 == x%40)
{
x = 0;
cout << endl;
}
if (line.substr(0,4).compare("addx") == 0)
{
if (accumulator >= x-1 && accumulator <= x+1)
{
cout << "#";
}
else
{
cout << " ";
}
accumulator += stoi(line.substr(5));
cycle++;
x++;
if (0 == x%40)
{
x = 0;
cout << endl;
}
}
}
}

93
Day8/part1.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string line;
getline(cin, line);
int length = line.size();
int* forest = new int[length*length];
int in_y = 0;
do
{
for(int i = 0; i < length; i++)
{
forest[i+in_y*length] = line[i]-'0';
}
++in_y;
} while(getline(cin, line));
bool* visible = new bool[length*length];
for(int i = 0; i < length*length; i++)
{
visible[i] = false;
}
for(int x = 0; x < length; x++)
{
//top down
int vis_height = -1;
for(int y = 0; y < length; y++)
{
if (forest[x+y*length] > vis_height)
{
vis_height = forest[x+y*length];
visible[x+y*length] = true;
}
}
//bottom up
vis_height = -1;
for(int y = length-1; y >= 0; y--)
{
if (forest[x+y*length] > vis_height)
{
vis_height = forest[x+y*length];
visible[x+y*length] = true;
}
}
}
for(int y = 0; y < length; y++)
{
//Left right
int vis_height = -1;
for(int x = 0; x < length; x++)
{
if (forest[x+y*length] > vis_height)
{
vis_height = forest[x+y*length];
visible[x+y*length] = true;
}
}
//Right left
vis_height = -1;
for(int x = length-1; x >= 0; x--)
{
if (forest[x+y*length] > vis_height)
{
vis_height = forest[x+y*length];
visible[x+y*length] = true;
}
}
}
int total_vis = 0;
for(int i = 0; i < length*length; i++)
{
if(visible[i])
{
++total_vis;
}
}
cout << total_vis << endl;
delete[] visible;
delete[] forest;
}

83
Day8/part2.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string line;
getline(cin, line);
int length = line.size();
int* forest = new int[length*length];
int in_y = 0;
do
{
for(int i = 0; i < length; i++)
{
forest[i+in_y*length] = line[i]-'0';
}
++in_y;
} while(getline(cin, line));
int best_vis = 0;
for(int y = 0; y < length; y++)
{
for(int x = 0; x < length; x++)
{
int north_vis = 0;
int south_vis = 0;
int west_vis = 0;
int east_vis = 0;
int cur_vis = forest[x+y*length];;
for(int j = y-1; j >= 0; j--)
{
north_vis++;
if (forest[x+j*length] >= cur_vis)
{
break;
}
}
for(int j = y+1; j < length; j++)
{
south_vis++;
if (forest[x+j*length] >= cur_vis)
{
break;
}
}
for(int i = x-1; i >= 0; i--)
{
west_vis++;
if (forest[i+y*length] >= cur_vis)
{
break;
}
}
for(int i = x+1; i<length; i++)
{
east_vis++;
if(forest[i+y*length] >= cur_vis)
{
break;
}
}
int total_vis = north_vis*south_vis*east_vis*west_vis;
if (total_vis > best_vis)
{
best_vis = total_vis;
}
}
}
cout << best_vis << endl;
delete[] forest;
}

94
Day9/part1.cpp Normal file
View File

@ -0,0 +1,94 @@
#include <iostream>
#include <string>
#include <set>
#include <utility>
using namespace std;
struct Rope
{
Rope(pair<int, int> head, pair<int, int> tail) : head{head}, tail{tail} {}
pair<int, int> head, tail;
};
Rope move_rope(Rope rope, int dx, int dy);
int main(int argc, char* argv[])
{
Rope rope({0,0}, {0,0});
set<pair<int, int>> visited;
string line;
while(getline(cin, line))
{
char dir = line[0];
int amount = stoi(line.substr(2));
int dx = (dir=='R')?1:((dir=='L')?-1:0);
int dy = (dir=='U')?1:((dir=='D')?-1:0);
for(int i = 0; i < amount; i++)
{
rope = move_rope(rope, dx, dy);
visited.insert(rope.tail);
}
}
cout << visited.size() << endl;
}
Rope move_rope(Rope rope, int dx, int dy)
{
rope.head.first += dx;
rope.head.second += dy;
if (rope.head.first == rope.tail.first)
{ //same x
if (rope.head.second > rope.tail.second)
{
rope.tail.second = rope.head.second-1;
}
else if (rope.head.second < rope.tail.second)
{
rope.tail.second = rope.head.second+1;
}
}
else if (rope.head.second == rope.tail.second)
{ //same y
if (rope.head.first > rope.tail.first)
{
rope.tail.first = rope.head.first-1;
}
else if (rope.head.first < rope.tail.first)
{
rope.tail.first = rope.head.first+1;
}
}
else
{ //diagonal
if (abs(rope.head.first-rope.tail.first) == 1 && abs(rope.head.second-rope.tail.second) == 1)
{
return rope;
}
if (rope.head.first-rope.tail.first > 0)
{
rope.tail.first++;
}
else
{
rope.tail.first--;
}
if (rope.head.second-rope.tail.second > 0)
{
rope.tail.second++;
}
else
{
rope.tail.second--;
}
}
return rope;
}

98
Day9/part2.cpp Normal file
View File

@ -0,0 +1,98 @@
#include <iostream>
#include <string>
#include <set>
#include <vector>
#include <utility>
using namespace std;
void move_rope(pair<int, int>* head, pair<int, int>* tail);
int main(int argc, char* argv[])
{
vector<pair<int,int>> rope;
for(int i = 0; i < 10; i++)
{
rope.push_back({0,0});
}
set<pair<int, int>> visited;
string line;
while(getline(cin, line))
{
char dir = line[0];
int amount = stoi(line.substr(2));
int dx = (dir=='R')?1:((dir=='L')?-1:0);
int dy = (dir=='U')?1:((dir=='D')?-1:0);
for(int i = 0; i < amount; i++)
{
rope[0].first += dx;
rope[0].second += dy;
vector<pair<int,int>>::iterator knot1 = rope.begin();
vector<pair<int,int>>::iterator knot2 = rope.begin()+1;
for(int j = 0; j < rope.size(); j++)
{
move_rope(&(*knot1), &(*knot2));
++knot1;
++knot2;
}
visited.insert(rope.back());
}
}
cout << visited.size() << endl;
}
void move_rope(pair<int, int>* head, pair<int, int>* tail)
{
if (head->first == tail->first)
{ //same x
if (head->second > tail->second)
{
tail->second = head->second-1;
}
else if (head->second < tail->second)
{
tail->second = head->second+1;
}
}
else if (head->second == tail->second)
{ //same y
if (head->first > tail->first)
{
tail->first = head->first-1;
}
else if (head->first < tail->first)
{
tail->first = head->first+1;
}
}
else
{ //diagonal
if (abs(head->first-tail->first) == 1 && abs(head->second-tail->second) == 1)
{
return;
}
if (head->first-tail->first > 0)
{
tail->first++;
}
else
{
tail->first--;
}
if (head->second-tail->second > 0)
{
tail->second++;
}
else
{
tail->second--;
}
}
}