Added some missing old files and new day2 of 2024.
This commit is contained in:
2
2023/1/Makefile
Normal file
2
2023/1/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
a.out: main.cpp
|
||||
clang++ -g -std=c++20 main.cpp
|
1000
2023/1/input.txt
Normal file
1000
2023/1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
7
2023/1/input2.txt
Normal file
7
2023/1/input2.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
134
2023/1/main.cpp
Normal file
134
2023/1/main.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#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);
|
||||
}
|
4
2023/1/sample.txt
Normal file
4
2023/1/sample.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
Reference in New Issue
Block a user