summaryrefslogtreecommitdiff
path: root/years/2015.cpp
diff options
context:
space:
mode:
authorroy <[email protected]>2026-07-14 10:15:08 +0300
committerroy <[email protected]>2026-07-14 10:15:08 +0300
commit100b893f9ee67c8a26dfd038785b59bede7fcf5a (patch)
tree1dd36b9db8991f7a4ae05ae047a893a405f4a9d3 /years/2015.cpp
parentb4470c5718736e75377791994dc25736bac7dc13 (diff)
Solved 2015 question 3.HEADmaster
Diffstat (limited to 'years/2015.cpp')
-rw-r--r--years/2015.cpp112
1 files changed, 100 insertions, 12 deletions
diff --git a/years/2015.cpp b/years/2015.cpp
index 67fe1a6..b894d55 100644
--- a/years/2015.cpp
+++ b/years/2015.cpp
@@ -1,8 +1,28 @@
#include "../aoc.h"
+#include <algorithm>
#include <iostream>
+#include <set>
#include <sstream>
#include <vector>
+std::vector<std::string> splitstring(std::string input, char splitter) {
+ std::stringstream splitterstream(input);
+ std::string segment;
+ std::vector<std::string> splatstring;
+
+ while (std::getline(splitterstream, segment, splitter)) {
+ splatstring.push_back(segment);
+ }
+
+ return splatstring;
+}
+
+template <typename T>
+bool vectorcontains(const std::vector<std::tuple<T, T>> &v,
+ const std::tuple<T, T> &x) {
+ return std::find(v.begin(), v.end(), x) != v.end();
+}
+
int y2015_q01_p01(std::string instructions) {
int floor = 0;
@@ -25,18 +45,6 @@ int y2015_q01_p02(std::string instructions) {
return -1;
}
-std::vector<std::string> splitstring(std::string input, char splitter) {
- std::stringstream splitterstream(input);
- std::string segment;
- std::vector<std::string> splatstring;
-
- while (std::getline(splitterstream, segment, splitter)) {
- splatstring.push_back(segment);
- }
-
- return splatstring;
-}
-
int y2015_q02_p01_instance(int l, int w, int h) {
return std::min(l * w, std::min(l * h, h * w)) + 2 * l * w + 2 * l * h +
2 * w * h;
@@ -79,3 +87,83 @@ int y2015_q02_p02() {
return sum;
}
+
+int y2015_q03_p01() {
+ std::string input;
+ getline(std::cin, input);
+
+ std::set<std::tuple<int, int>> board;
+ board.insert({0, 0});
+ int x, y;
+
+ for (const char &c : input) {
+ switch (c) {
+ case '>':
+ x++;
+ break;
+ case '<':
+ x--;
+ break;
+ case '^':
+ y++;
+ break;
+ case 'v':
+ y--;
+ break;
+ }
+ board.insert({x, y});
+ }
+
+ return static_cast<int>(board.size());
+}
+
+int y2015_q03_p02() {
+ std::string input;
+ std::cout << "Warning! This input might need to be hardcoded depending on "
+ "your terminal's input length limit.";
+ getline(std::cin, input);
+
+ std::set<std::tuple<int, int>> board;
+ board.insert({0, 0});
+ bool bot = false;
+ int x = 0, y = 0, x2 = 0, y2 = 0;
+
+ for (const char &c : input) {
+ if (bot) {
+ switch (c) {
+ case '>':
+ x2++;
+ break;
+ case '<':
+ x2--;
+ break;
+ case '^':
+ y2++;
+ break;
+ case 'v':
+ y2--;
+ break;
+ }
+ board.insert({x2, y2});
+ } else {
+ switch (c) {
+ case '>':
+ x++;
+ break;
+ case '<':
+ x--;
+ break;
+ case '^':
+ y++;
+ break;
+ case 'v':
+ y--;
+ break;
+ }
+ board.insert({x, y});
+ }
+ bot = !bot;
+ }
+
+ return static_cast<int>(board.size());
+}