Skip to content

Commit

Permalink
Solution for 2015 Day 3 part 2 (C++)
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Lukas <[email protected]>
  • Loading branch information
SebaLukas committed Jan 11, 2023
1 parent 70646bb commit 357bc0a
Showing 1 changed file with 74 additions and 4 deletions.
78 changes: 74 additions & 4 deletions 2015/Day3/day3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <vector>
#include <algorithm>

int main ()
{

void part1() {
std::ifstream t("input.txt");
std::string s_input((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
Expand Down Expand Up @@ -43,6 +41,78 @@ int main ()

positions.erase(uni, positions.end());

std::cout << positions.size() << " houses receive at least one present!\n";
std::cout << "Part1: " << positions.size() << " houses receive at least one present!\n";
}

void part2() {
std::ifstream t("input.txt");
std::string s_input((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());

std::vector<int> positions;
int position_x = 0;
int position_y = 0;
int position_robo_x = 0;
int position_robo_y = 0;

positions.push_back(0); //First home

bool santa = true;

for(char& c : s_input) {

switch (c)
{
case '>':
if (santa) {
position_x++;
} else {
position_robo_x++;
}
break;
case '<':
if (santa) {
position_x--;
} else {
position_robo_x--;
}
break;
case '^':
if (santa) {
position_y++;
} else {
position_robo_y++;
}
break;
case 'v':
if (santa) {
position_y--;
} else {
position_robo_y--;
}
break;
}

if (santa) {
positions.push_back(100*position_x + position_y);
santa = false;
} else {
positions.push_back(100*position_robo_x + position_robo_y);
santa = true;
}
}

std::sort(positions.begin(), positions.end());

auto uni = std::unique(positions.begin(), positions.end());

positions.erase(uni, positions.end());

std::cout << "Part2: " << positions.size() << " houses receive at least one present!\n";
}

int main ()
{
part1();
part2();
}

0 comments on commit 357bc0a

Please sign in to comment.