Fuck you Logan

This commit is contained in:
Thomas Muller 2022-12-02 18:41:28 -05:00
commit 6a3d307033
Signed by: thomas
GPG key ID: AF006EB730564952
9 changed files with 5072 additions and 0 deletions

2255
day_1/input Normal file

File diff suppressed because it is too large Load diff

BIN
day_1/main Executable file

Binary file not shown.

41
day_1/main.cpp Normal file
View file

@ -0,0 +1,41 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
int main(int argc, char *argv[]) {
if(argc != 2) {
std::cerr << "Usage " << argv[0] << " <input>" << std::endl;
return -1;
}
std::ifstream file;
file.open(argv[1]);
std::string line;
std::vector<int> groups;
int sum = 0;
while(file) {
std::getline(file, line);
std::cout << "got " << line << std::endl;
if(line == "") {
if(sum > 0)
groups.push_back(sum);
sum = 0;
} else {
sum += std::stoi(line);
}
}
for(int sum : groups) {
std::cout << "sum " << sum << std::endl;
}
std::cout << "Parsed " << groups.size() << " groups" << std::endl;
std::cout << "Max is " << *std::max_element(groups.begin(), groups.end()) << std::endl;
return 0;
}

52
day_1/main2.cpp Normal file
View file

@ -0,0 +1,52 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
int main(int argc, char *argv[]) {
if(argc != 2) {
std::cerr << "Usage " << argv[0] << " <input>" << std::endl;
return -1;
}
std::ifstream file;
file.open(argv[1]);
std::string line;
std::vector<int> groups;
int sum = 0;
while(file) {
std::getline(file, line);
std::cout << "got " << line << std::endl;
if(line == "") {
if(sum > 0)
groups.push_back(sum);
sum = 0;
} else {
sum += std::stoi(line);
}
}
for(int sum : groups) {
std::cout << "sum " << sum << std::endl;
}
std::cout << "Parsed " << groups.size() << " groups" << std::endl;
int maxs[3];
auto max = std::max_element(groups.begin(), groups.end());
maxs[0] = *max;
groups.erase(max);
max = std::max_element(groups.begin(), groups.end());
maxs[1] = *max;
groups.erase(max);
max = std::max_element(groups.begin(), groups.end());
maxs[2] = *max;
groups.erase(max);
std::cout << "Max is " << maxs[0] + maxs[1] + maxs[2] << std::endl;
return 0;
}

2501
day_2/input Normal file

File diff suppressed because it is too large Load diff

BIN
day_2/main Executable file

Binary file not shown.

107
day_2/main.cpp Normal file
View file

@ -0,0 +1,107 @@
#include <utility>
#include <iostream>
#include <fstream>
#include <vector>
enum class Play : int {
FUCKYOU = 0,
ROCK = 1,
PAPER = 2,
SCISSORS = 3,
};
enum class Result : char {
WIN = 'W',
LOSS = 'L',
TIE = 'T',
};
typedef std::pair<Play, Play> game_t;
Play char_to_play(char c) {
switch(c) {
case 'A':
case 'X':
return Play::ROCK;
case 'B':
case 'Y':
return Play::PAPER;
case 'C':
case 'Z':
return Play::SCISSORS;
}
return Play::FUCKYOU;
}
std::vector<game_t> read_file(std::string path) {
std::fstream file;
file.open(path);
std::string line;
std::vector<game_t> games;
while(file) {
std::getline(file, line);
if(line == "")
break;
auto opponent = char_to_play(line[0]);
auto mine = char_to_play(line[2]);
games.push_back(std::make_pair(opponent, mine));
}
return games;
}
Result judge_game(game_t game) {
auto [opponent, me] = game;
if(opponent == me)
return Result::TIE;
if(opponent == Play::ROCK && me == Play::SCISSORS)
return Result::LOSS;
if(opponent == Play::SCISSORS && me == Play::ROCK)
return Result::WIN;
if((int)opponent > (int)me)
return Result::LOSS;
else
return Result::WIN;
}
int score_game(game_t game, Result result) {
int score = 0;
if(result == Result::WIN)
score = 6;
else if(result == Result::TIE)
score = 3;
score += (int)game.second;
return score;
}
int main(int argc, char *argv[]) {
auto games = read_file(argv[1]);
int total = 0;
for(auto game : games) {
auto [opponent, me] = game;
std::cout << (int)opponent << " " << (int)me << std::endl;
auto result = judge_game(game);
std::cout << (char)result << std::endl;
int score = score_game(game, result);
std::cout << score << std::endl;
total += score;
}
std::cout << total << std::endl;
return 0;
}

BIN
day_2/main2 Executable file

Binary file not shown.

116
day_2/main2.cpp Normal file
View file

@ -0,0 +1,116 @@
#include <utility>
#include <iostream>
#include <fstream>
#include <vector>
enum class Play : int {
ROCK = 1,
PAPER = 2,
SCISSORS = 3,
};
enum class Result : char {
WIN = 'W',
LOSS = 'L',
TIE = 'T',
};
typedef std::pair<Play, Result> game_t;
Play char_to_play(char c) {
switch(c) {
case 'A':
return Play::ROCK;
case 'B':
return Play::PAPER;
case 'C':
return Play::SCISSORS;
}
throw;
}
Result char_to_result(char c) {
switch(c) {
case 'X':
return Result::LOSS;
case 'Y':
return Result::TIE;
case 'Z':
return Result::WIN;
}
throw;
}
Play cheat(Play opponent, Result result) {
if(result == Result::WIN) {
if(opponent == Play::ROCK) return Play::PAPER;
if(opponent == Play::PAPER) return Play::SCISSORS;
if(opponent == Play::SCISSORS) return Play::ROCK;
} else if(result == Result::LOSS) {
if(opponent == Play::ROCK) return Play::SCISSORS;
if(opponent == Play::PAPER) return Play::ROCK;
if(opponent == Play::SCISSORS) return Play::PAPER;
} else {
return opponent;
}
throw;
}
std::vector<game_t> read_file(std::string path) {
std::fstream file;
file.open(path);
std::string line;
std::vector<game_t> games;
while(file) {
std::getline(file, line);
if(line == "")
break;
auto opponent = char_to_play(line[0]);
auto mine = char_to_result(line[2]);
games.push_back(std::make_pair(opponent, mine));
}
return games;
}
int score_game(Play play, Result result) {
int score = 0;
if(result == Result::WIN)
score = 6;
else if(result == Result::TIE)
score = 3;
score += (int)play;
return score;
}
int main(int argc, char *argv[]) {
auto games = read_file(argv[1]);
int total = 0;
for(auto game : games) {
auto [opponent, result] = game;
auto me = cheat(opponent, result);
std::cout << (int)opponent << " " << (int)me << std::endl;
int score = score_game(me, result);
std::cout << score << std::endl;
total += score;
}
std::cout << total << std::endl;
return 0;
}