From 357bc0a1caff585578cce0883e72b12ad8ea6f9d Mon Sep 17 00:00:00 2001 From: Sebastian Lukas Date: Wed, 11 Jan 2023 17:27:46 +0100 Subject: [PATCH] Solution for 2015 Day 3 part 2 (C++) Signed-off-by: Sebastian Lukas --- 2015/Day3/day3.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/2015/Day3/day3.cpp b/2015/Day3/day3.cpp index 15e1a10..fda9d34 100644 --- a/2015/Day3/day3.cpp +++ b/2015/Day3/day3.cpp @@ -5,9 +5,7 @@ #include #include -int main () -{ - +void part1() { std::ifstream t("input.txt"); std::string s_input((std::istreambuf_iterator(t)), std::istreambuf_iterator()); @@ -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(t)), + std::istreambuf_iterator()); + + std::vector 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(); } \ No newline at end of file