Added year directories for advent of code

This commit is contained in:
2022-12-02 13:37:41 +01:00
parent 27d00340b9
commit 6c9525248e
31 changed files with 275 additions and 0 deletions

View File

@@ -0,0 +1,210 @@
#include <bits/stdc++.h>
using namespace std;
string file_name = "8.txt";
// string file_name = "test_text.txt";
vector<pair<array<string, 10>, array<string, 4>>> input_text;
pair<array<string, 10>, array<string, 4>> parse_line(const string &s){
istringstream ss(s);
pair<array<string, 10>, array<string, 4>> result;
string word;
for(size_t i = 0; i < 10; ++i){
ss >> word;
result.first[i] = word;
}
// There should be a pipe | here, which doesn't need to be put into the strings
ss >> word;
for(size_t i = 0; i < 4; ++i){
ss >> word;
result.second[i] = word;
}
return result;
}
map<string, int> identify_digits(const array<string, 10> &digits){
map<int, string> digit_strings;
vector<string> size_fives;
vector<string> size_six;
for(size_t i = 0; i < 10; ++i){
size_t ds = digits[i].size();
if(ds == 2){
digit_strings[1] = digits[i];
} else if(ds == 3){
digit_strings[7] = digits[i];
} else if(ds == 4){
digit_strings[4] = digits[i];
} else if(ds == 7){
digit_strings[8] = digits[i];
} else if(ds == 5){
size_fives.push_back(digits[i]);
} else if(ds == 6){
size_six.push_back(digits[i]);
}
}
vector<set<char>> size_fives_sets(3);
for(int i = 0; i < 3; ++i){
for(char &c : size_fives[i]){
size_fives_sets[i].insert(c);
}
// We know that 2 is the only digit that combines with 4 to be 8
for(char &c : digit_strings[4]){
size_fives_sets[i].insert(c);
}
if(size_fives_sets[i].size() == 7){
digit_strings[2] = size_fives[i];
size_fives.erase(size_fives.begin() + i);
break;
}
}
// Now do kind of the same thing but for 5 and 3, combined with 7
set<char> three_or_five;
for(char &c : size_fives[0]){
three_or_five.insert(c);
}
for(char &c : digit_strings[7]){
three_or_five.insert(c);
}
// So in the case that it not of size 5, which means it is the number 5
if(three_or_five.size() != 5){
swap(size_fives[0], size_fives[1]);
}
digit_strings[3] = size_fives[0];
digit_strings[5] = size_fives[1];
vector<set<char>> size_six_sets(3);
for(int i = 0; i < 3; ++i){
for(char &c : size_six[i]){
size_six_sets[i].insert(c);
}
// Only 6 and 7 together form 8 from all the six sized digits
for(char &c : digit_strings[7]){
size_six_sets[i].insert(c);
}
if(size_six_sets[i].size() == 7){
digit_strings[6] = size_six[i];
size_six.erase(size_six.begin() + i);
break;
}
}
// Now we only need 9 and 0, which can be distinguished by combining with 4
set<char> nine_or_zero;
for(char &c : size_six[0]){
nine_or_zero.insert(c);
}
for(char &c : digit_strings[4]){
nine_or_zero.insert(c);
}
// In the case that the size is 7, which means it combines to an eight
if(nine_or_zero.size() != 7){
swap(size_six[0], size_six[1]);
}
digit_strings[0] = size_six[0];
digit_strings[9] = size_six[1];
for(auto &s : digit_strings){
sort(s.second.begin(), s.second.end());
}
map<string, int> result;
for(auto &d : digit_strings){
result[d.second] = d.first;
}
return result;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ifstream file_stream;
file_stream.open(file_name);
if(file_stream.is_open()){
string line;
while(getline(file_stream, line)){
input_text.push_back(parse_line(line));
}
file_stream.close();
}
// Testing the input parsing
// cout << input_text.size() << endl;
// for(const auto &line : input_text){
// for(auto word : line.first){
// cout << word << " ";
// }
// cout << "| ";
// for(auto word : line.second){
// cout << word << " ";
// }
// cout << endl;
// }
int result = 0;
for(const auto &line : input_text){
for(size_t i = 0; i < 4; ++i){
size_t w = line.second[i].size();
if(w == 2 || w == 4 || w == 3 || w == 7){
result++;
}
}
}
cout << result << endl;
result = 0;
for(auto &line : input_text){
auto digit_map = identify_digits(line.first);
int acc = 0;
for(int i = 0; i < 4; ++i){
string s = line.second[i];
sort(s.begin(), s.end());
acc *= 10;
acc += digit_map[s];
}
result += acc;
}
cout << result << endl;
cout << flush;
return 0;
}