Skip to content

Instantly share code, notes, and snippets.

@thamstras
Created December 2, 2018 15:40
Show Gist options
  • Save thamstras/1a4fabd1c8822184b6df872ce6abc0be to your computer and use it in GitHub Desktop.
Save thamstras/1a4fabd1c8822184b6df872ce6abc0be to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <intrin.h>
int charToInt(char c)
{
int t = (int)c;
if (t > 122) return -1;
if (t >= 97) return (t - 97);
if (t > 90) return -1;
if (t >= 65) return (t - 41);
return -1;
}
int main(int argc, char* argv[])
{
//__debugbreak();
if (argc < 2)
{
std::cout << "No file supplied." << std::endl;
return 1;
}
if (argc > 2)
{
std::cout << "Too many inputs (quotes needed?)" << std::endl;
return 1;
}
std::ifstream file(argv[1]);
if (file.is_open())
{
/* //Part 1
int seen[26];
bool twoSame;
bool threeSame;
int numTwos = 0;
int numThrees = 0;
std::string line;
while (std::getline(file, line))
{
for (int i = 0; i < 26; i++) seen[i] = 0;
twoSame = false;
threeSame = false;
for each (char c in line)
{
int i = charToInt(c);
if (i < 0) continue;
seen[i]++;
}
for (int i = 0; i < 26; i++)
{
if (seen[i] == 2) twoSame = true;
if (seen[i] == 3) threeSame = true;
}
if (twoSame) numTwos++;
if (threeSame) numThrees++;
}
int checksum = numThrees * numTwos;
std::cout << "Checksum: " << checksum << std::endl;
return 0;
*/
std::vector<std::string> lines = std::vector<std::string>();
std::string s;
while (std::getline(file, s)) lines.push_back(s);
file.close();
int nLines = lines.size();
bool found = false;
int foundA, foundB;
for (int i = 0; i < (nLines - 1); i++)
{
std::string& iLine = lines[i];
for (int j = (i + 1); j < nLines; j++)
{
std::string& jLine = lines[j];
int diffs = 0;
for (int k = 0; k < jLine.length(); k++)
{
if (iLine[k] != jLine[k]) diffs++;
}
if (diffs == 1)
{
found = true;
foundA = i;
foundB = j;
break;
}
}
if (found) break;
}
std::string& aLine = lines[foundA];
std::string& bLine = lines[foundB];
std::cout << "matching chars: ";
for (int c = 0; c < aLine.length(); c++)
{
if (aLine[c] == bLine[c]) std::cout << aLine[c];
}
std::cout << std::endl;
return 0;
}
else
{
std::cout << "Failed to open file!" << std::endl;
return 1;
}
std::cout << "An unknown error ocurred!" << std::endl;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment