Skip to content

Instantly share code, notes, and snippets.

@Azoy
Created March 7, 2018 02:16
Show Gist options
  • Select an option

  • Save Azoy/b7270b60f2e7b23f107a3e9fbfa07d4c to your computer and use it in GitHub Desktop.

Select an option

Save Azoy/b7270b60f2e7b23f107a3e9fbfa07d4c to your computer and use it in GitHub Desktop.
Advent of Code 2017
#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