Retried 2023 Day 1 Part 2 after Becky mentioned we never did it. It isn't optimized, but it's done.

This commit is contained in:
David Vereb 2024-12-06 16:49:48 -05:00
parent 86476f7ff0
commit 156f1e7dac
9 changed files with 1247 additions and 112 deletions

View File

@ -1,2 +1,2 @@
a.out: main.cpp a.out: main.cpp
clang++ -g -std=c++20 main.cpp clang++ -std=c++2b -g -O0 main.cpp

View File

@ -1,134 +1,126 @@
#include <cctype> #include <algorithm>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector> #include <vector>
#define INPUT_FILE "input2.txt"
int ParseNumLine1(const std::string &line);
int ParseNumLine2(const std::string &line);
int main() int main()
{ {
std::ifstream ifs(INPUT_FILE); const std::string filename = "data.txt";
std::ifstream ifs(filename);
if(!ifs.is_open()) if(!ifs.is_open())
{ {
std::cerr << "Couldn't open " INPUT_FILE "." << std::endl; std::cerr << "Missing " << filename << "." << std::endl;
return -1; return -1;
} }
long result1 = 0; unsigned long total = 0;
long result2 = 0; unsigned long total_pt2 = 0;
for(std::string line = ""; std::getline(ifs, line); )
std::map<std::string, std::string> values =
{ {
result1 += ParseNumLine1(line); { "0", "0" },
result2 += ParseNumLine2(line); { "1", "1" },
} { "2", "2" },
{ "3", "3" },
{ "4", "4" },
{ "5", "5" },
{ "6", "6" },
{ "7", "7" },
{ "8", "8" },
{ "9", "9" },
// { "zero", 0 },
{ "one", "1" },
{ "two", "2" },
{ "three", "3" },
{ "four", "4" },
{ "five", "5" },
{ "six", "6" },
{ "seven", "7" },
{ "eight", "8" },
{ "nine", "9" }
};
std::cout << "Part 1 Result: " << result1 << std::endl; auto GetDigit = [&](const std::string &line, int i, bool ascending, std::string &result) {
std::cout << "Part 2 Result: " << result2 << std::endl; std::vector<std::string> tests;
if(ascending)
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) tests.push_back(line.substr(i, 1)); // single digits
{ if(i < line.size() - 2)
found[0] = ch; tests.push_back(line.substr(i, 3)); // three-letter words
found_first = true; if(i < line.size() - 3)
} tests.push_back(line.substr(i, 4)); // four-letter words
if(i < line.size() - 4)
// same as first, if only one tests.push_back(line.substr(i, 5)); // five-letter words
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 else
{ {
const static std::vector<std::string> available = { if(i < line.size() - 4)
"one", tests.push_back(line.substr(i, 5)); // five-letter words
"two", if(i < line.size() - 3)
"three", tests.push_back(line.substr(i, 4)); // four-letter words
"four", if(i < line.size() - 2)
"five", tests.push_back(line.substr(i, 3)); // three-letter words
"six", tests.push_back(line.substr(i, 1)); // single digits
"seven", }
"eight",
"nine",
};
int num = 0; for(auto test : tests)
for(auto i = 0; i < line.size(); ++i) {
if(values.contains(test))
{ {
for(auto w = 0; w < available.size(); ++w) result = values.at(test);
{ return true;
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;
}
} }
} }
return false;
};
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
continue;
std::string first = "";
std::string last = "";
// first
for(auto i = 0; i < line.size(); ++i)
{
std::string result;
if(GetDigit(line, i, true, result))
{
first = result;
break;
}
}
if(first == "")
std::cerr << "Missing digit in line: " << line << std::endl;
// last
for(auto i = 0; i < line.size(); ++i)
{
std::string result;
if(GetDigit(line, line.size() - i - 1, false, result))
{
last = result;
break;
}
}
if(last == "")
std::cerr << "Missing digit in line: " << line << std::endl;
std::string val = first + last;
int val_int = std::atoi(val.c_str());
std::cout << val_int << std::endl;
total_pt2 += val_int;
} }
std::cout << "Return: " << found << " == " << std::atoi(found) << std::endl;
return std::atoi(found); std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl;
return 0;
} }

2
2023/1_old/Makefile Normal file
View File

@ -0,0 +1,2 @@
a.out: main.cpp
clang++ -g -std=c++20 main.cpp

1000
2023/1_old/input.txt Normal file

File diff suppressed because it is too large Load Diff

7
2023/1_old/input2.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

134
2023/1_old/main.cpp Normal file
View 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);
}