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:
parent
86476f7ff0
commit
156f1e7dac
@ -1,2 +1,2 @@
|
||||
a.out: main.cpp
|
||||
clang++ -g -std=c++20 main.cpp
|
||||
clang++ -std=c++2b -g -O0 main.cpp
|
||||
|
214
2023/1/main.cpp
214
2023/1/main.cpp
@ -1,134 +1,126 @@
|
||||
#include <cctype>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#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);
|
||||
const std::string filename = "data.txt";
|
||||
std::ifstream ifs(filename);
|
||||
if(!ifs.is_open())
|
||||
{
|
||||
std::cerr << "Couldn't open " INPUT_FILE "." << std::endl;
|
||||
std::cerr << "Missing " << filename << "." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
long result1 = 0;
|
||||
long result2 = 0;
|
||||
for(std::string line = ""; std::getline(ifs, line); )
|
||||
unsigned long total = 0;
|
||||
unsigned long total_pt2 = 0;
|
||||
|
||||
std::map<std::string, std::string> values =
|
||||
{
|
||||
result1 += ParseNumLine1(line);
|
||||
result2 += ParseNumLine2(line);
|
||||
}
|
||||
{ "0", "0" },
|
||||
{ "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;
|
||||
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))
|
||||
auto GetDigit = [&](const std::string &line, int i, bool ascending, std::string &result) {
|
||||
std::vector<std::string> tests;
|
||||
if(ascending)
|
||||
{
|
||||
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;
|
||||
tests.push_back(line.substr(i, 1)); // single digits
|
||||
if(i < line.size() - 2)
|
||||
tests.push_back(line.substr(i, 3)); // three-letter words
|
||||
if(i < line.size() - 3)
|
||||
tests.push_back(line.substr(i, 4)); // four-letter words
|
||||
if(i < line.size() - 4)
|
||||
tests.push_back(line.substr(i, 5)); // five-letter words
|
||||
}
|
||||
else
|
||||
{
|
||||
const static std::vector<std::string> available = {
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"four",
|
||||
"five",
|
||||
"six",
|
||||
"seven",
|
||||
"eight",
|
||||
"nine",
|
||||
};
|
||||
if(i < line.size() - 4)
|
||||
tests.push_back(line.substr(i, 5)); // five-letter words
|
||||
if(i < line.size() - 3)
|
||||
tests.push_back(line.substr(i, 4)); // four-letter words
|
||||
if(i < line.size() - 2)
|
||||
tests.push_back(line.substr(i, 3)); // three-letter words
|
||||
tests.push_back(line.substr(i, 1)); // single digits
|
||||
}
|
||||
|
||||
int num = 0;
|
||||
for(auto i = 0; i < line.size(); ++i)
|
||||
for(auto test : tests)
|
||||
{
|
||||
if(values.contains(test))
|
||||
{
|
||||
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;
|
||||
}
|
||||
result = values.at(test);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
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
2
2023/1_old/Makefile
Normal file
@ -0,0 +1,2 @@
|
||||
a.out: main.cpp
|
||||
clang++ -g -std=c++20 main.cpp
|
1000
2023/1_old/input.txt
Normal file
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
7
2023/1_old/input2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
134
2023/1_old/main.cpp
Normal file
134
2023/1_old/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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user