Created
March 7, 2018 02:16
-
-
Save Azoy/b7270b60f2e7b23f107a3e9fbfa07d4c to your computer and use it in GitHub Desktop.
Advent of Code 2017
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| int solve_capcha(int *ptr, int size) { | |
| int tmp = 0; | |
| for (int i = 0; i < size; i++) { | |
| int index = i - 1; | |
| if (i == 0) { | |
| index = size - 1; | |
| } | |
| int a = ptr[index]; | |
| int b = ptr[i]; | |
| if (a == b) { | |
| tmp += a; | |
| } | |
| } | |
| return tmp; | |
| } | |
| int solve_capcha2(int *ptr, int size) { | |
| int halfway = size / 2; | |
| int tmp = 0; | |
| for (int i = 0; i < size; i++) { | |
| int index = i + halfway; | |
| if (i == size - 1) { | |
| index = halfway - 1; | |
| } | |
| int a = ptr[index]; | |
| int b = ptr[i]; | |
| if (a == b) { | |
| tmp += a + b; | |
| } | |
| } | |
| return tmp; | |
| } | |
| int main() { | |
| std::string input; | |
| getline (std::cin, input); | |
| std::cout << "Input: " << input << std::endl; | |
| std::cout << "Input Size: " << input.size() << std::endl; | |
| int *ptr = new int[input.size()]; | |
| if (ptr == nullptr) { | |
| std::cout << "We failed to allocate enough memory" << std::endl; | |
| return 1; | |
| } | |
| for (int i = 0; i < static_cast<int>(input.size()); i++) { | |
| //std::cout << static_cast<int>(input[i]) << std::endl; | |
| ptr[i] = static_cast<int>(input[i]) - '0'; | |
| //std::cout << ptr[i] << std::endl; | |
| } | |
| std::cout << "Capcha: " << solve_capcha(ptr, input.size()) << std::endl; | |
| std::cout << "Capcha 2: " << solve_capcha2(ptr, input.size()) << std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment