Added day 2 and 3, but didn't do them in rust like I wanted to.

This commit is contained in:
David Vereb 2022-12-05 11:08:48 -05:00
parent 209aa39edb
commit be4347ab5f
4 changed files with 2921 additions and 1 deletions

2500
2022/2/data.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,12 @@ enum RPS {
Scissors, Scissors,
}; };
enum Result {
Lose = 1,
Draw,
Win,
};
RPS ToRPS(char c) RPS ToRPS(char c)
{ {
if(c == 'A' || c == 'X') if(c == 'A' || c == 'X')
@ -46,6 +52,28 @@ bool Won(RPS us, RPS them)
return false; return false;
} }
RPS Which(RPS them, Result desired)
{
if(desired == Result::Draw)
return them; // same
switch(them)
{
case RPS::Rock:
if(desired == Result::Lose)
return RPS::Scissors;
return RPS::Paper;
case RPS::Paper:
if(desired == Result::Lose)
return RPS::Rock;
return RPS::Scissors;
case RPS::Scissors:
if(desired == Result::Lose)
return RPS::Paper;
return RPS::Rock;
}
}
int Points(RPS us, RPS them) int Points(RPS us, RPS them)
{ {
int rtn = 0; int rtn = 0;
@ -73,16 +101,27 @@ int main()
} }
unsigned long total = 0; unsigned long total = 0;
unsigned long new_total = 0;
for(std::string line; std::getline(ifs, line); ) for(std::string line; std::getline(ifs, line); )
{ {
char them = line[0]; char them = line[0];
char us = line[2]; char us = line[2];
Result r = Result::Lose;
if(us == 'Y')
r = Result::Draw;
else if(us == 'Z')
r = Result::Win;
RPS todo = Which(ToRPS(them), r);
total += Points(ToRPS(us), ToRPS(them)); total += Points(ToRPS(us), ToRPS(them));
new_total += Points(todo, ToRPS(them));
} }
std::cout << " Total: " << total << std::endl; std::cout << " Total: " << total << std::endl;
std::cout << "New Total: " << new_total << std::endl;
return 0; return 0;
} }

300
2022/3/data.txt Normal file
View File

@ -0,0 +1,300 @@
vGFhvGvvSdfwqhqvmCPnlFPnCNPcCFcWcr
ZbWZDMgsTHsrNNLJcJnsJl
HbBWQgZVZZBzbgZphwjqpmmVfdGmjG
vvCJLGnthChvtrvCCnRbTRqRPRBtb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SbnmZPZPNpNGMpJNzzNrGJpvhsshMpFs
mwnZcbmmStbVtVjbZVlcLTBlcLCRHRDWCWWW

81
2022/3/main.cpp Normal file
View File

@ -0,0 +1,81 @@
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
int score(char c)
{
if(c >= 'a' && c <= 'z')
return (c - 'a') + 1;
return (c - 'A') + 1 + 26;
}
int main()
{
std::ifstream ifs("data.txt");
if(!ifs.is_open())
{
std::cerr << "Missing data.txt." << std::endl;
return -1;
}
unsigned long total = 0;
unsigned long total_pt2 = 0;
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
continue;
const size_t half = line.size() / 2;
std::string right = line.substr(half);
for(int i = 0; i < half; ++i)
{
if(right.contains(line[i]))
{
total += score(line[i]);
break;
}
}
// Part 2:
static std::vector<std::string> lines;
lines.push_back(line);
if(lines.size() == 3)
{
// get all letters it could be from first line
std::set<char> options;
for(const char c : lines[0])
options.insert(c);
// remove if not a duplicate
std::set<char> to_remove;
for(auto i = 1; i < 3; ++i) // remaining two lines
{
for(const char c : options)
if(!lines[i].contains(c))
to_remove.insert(c);
for(const char c : to_remove)
options.erase(c);
}
if(options.size() != 1)
{
std::cerr << "Error: More than one remaining option: ";
for(const char c : options)
std::cout << c << ",";
exit(-1);
}
total_pt2 += score(*(options.begin()));
lines.clear(); // reset
}
}
std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl;
return 0;
}