135 lines
2.5 KiB
C++
135 lines
2.5 KiB
C++
|
#include <cctype>
|
||
|
#include <fstream>
|
||
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
|
||
|
#define INPUT_FILE "input2.txt"
|
||
|
|
||
|
int ParseNumLine1(const std::string &line);
|
||
|
int ParseNumLine2(const std::string &line);
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
std::ifstream ifs(INPUT_FILE);
|
||
|
if(!ifs.is_open())
|
||
|
{
|
||
|
std::cerr << "Couldn't open " INPUT_FILE "." << std::endl;
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
long result1 = 0;
|
||
|
long result2 = 0;
|
||
|
for(std::string line = ""; std::getline(ifs, line); )
|
||
|
{
|
||
|
result1 += ParseNumLine1(line);
|
||
|
result2 += ParseNumLine2(line);
|
||
|
}
|
||
|
|
||
|
std::cout << "Part 1 Result: " << result1 << std::endl;
|
||
|
std::cout << "Part 2 Result: " << result2 << std::endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int ParseNumLine1(const std::string &line)
|
||
|
{
|
||
|
char found[3] = { '\0', '\0', '\0' };
|
||
|
bool found_first = false;
|
||
|
for(auto &ch : line)
|
||
|
{
|
||
|
if(std::isdigit(ch))
|
||
|
{
|
||
|
if(!found_first)
|
||
|
{
|
||
|
found[0] = ch;
|
||
|
found_first = true;
|
||
|
}
|
||
|
|
||
|
// same as first, if only one
|
||
|
found[1] = ch;
|
||
|
}
|
||
|
}
|
||
|
return std::atoi(found);
|
||
|
}
|
||
|
|
||
|
int ParseNumLine2(const std::string &line)
|
||
|
{
|
||
|
char found[3] = { '\0', '\0', '\0' };
|
||
|
bool found_first = false;
|
||
|
for(auto &ch : line)
|
||
|
{
|
||
|
if(std::isdigit(ch))
|
||
|
{
|
||
|
if(!found_first)
|
||
|
{
|
||
|
found[0] = ch;
|
||
|
found_first = true;
|
||
|
}
|
||
|
|
||
|
// same as first, if only one
|
||
|
found[1] = ch;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
const static std::vector<std::string> available = {
|
||
|
"one",
|
||
|
"two",
|
||
|
"three",
|
||
|
"four",
|
||
|
"five",
|
||
|
"six",
|
||
|
"seven",
|
||
|
"eight",
|
||
|
"nine",
|
||
|
};
|
||
|
|
||
|
int num = 0;
|
||
|
for(auto i = 0; i < line.size(); ++i)
|
||
|
{
|
||
|
for(auto w = 0; w < available.size(); ++w)
|
||
|
{
|
||
|
const std::string &word = available[w];
|
||
|
if(line.size() - i < word.size())
|
||
|
continue; // not enough letters for this word
|
||
|
|
||
|
bool word_matches = true;
|
||
|
for(auto letter = 0; i + letter < line.size() && word_matches; ++letter)
|
||
|
{
|
||
|
if(line[i + letter] != word[letter])
|
||
|
{
|
||
|
word_matches = false;
|
||
|
break;
|
||
|
}
|
||
|
if(letter == word.size() - 1)
|
||
|
{
|
||
|
num = w + 1;
|
||
|
// std::cout << "Found: ";
|
||
|
// for(auto a = 0; a <= letter; ++a)
|
||
|
// std::cout << line[i + a];
|
||
|
// std::cout << " == " << available[w] << " @ " << letter << std::endl;
|
||
|
break; // true!
|
||
|
}
|
||
|
}
|
||
|
if(word_matches)
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if(num)
|
||
|
{
|
||
|
if(!found_first)
|
||
|
{
|
||
|
found[0] = num + '0';
|
||
|
found_first = true;
|
||
|
}
|
||
|
|
||
|
found[1] = num + '0';
|
||
|
|
||
|
num = 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
std::cout << "Return: " << found << " == " << std::atoi(found) << std::endl;
|
||
|
return std::atoi(found);
|
||
|
}
|