2022-12-20 15:46:56 -05:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2024-12-02 14:07:39 -05:00
|
|
|
enum Operation {
|
|
|
|
ADD,
|
|
|
|
MULTIPLY,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Monkey {
|
|
|
|
public:
|
|
|
|
Monkey(const std::vector<int> &starting_items, Operation operation_type, int operation_amount,
|
|
|
|
int division_by, int true_monkey, int false_monkey)
|
|
|
|
: items(starting_items), operation_type(operation_type), operation_amount(operation_amount),
|
|
|
|
division_by(division_by), true_monkey(true_monkey), false_monkey(false_monkey)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream &out, const Monkey &monkey);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<int> items;
|
|
|
|
|
|
|
|
Operation operation_type;
|
|
|
|
int operation_amount;
|
|
|
|
|
|
|
|
int division_by;
|
|
|
|
|
|
|
|
int true_monkey;
|
|
|
|
int false_monkey;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<int> ParseItems(const std::string &line);
|
|
|
|
std::pair<Operation, int> ParseOperation(const std::string &line);
|
|
|
|
int ParseDivisibleBy(const std::string &line);
|
|
|
|
int ParseTrue(const std::string &line);
|
|
|
|
int ParseFalse(const std::string &line);
|
|
|
|
|
2022-12-20 15:46:56 -05:00
|
|
|
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;
|
|
|
|
|
2024-12-02 14:07:39 -05:00
|
|
|
std::vector<Monkey> monkeys;
|
|
|
|
int line_num = 0;
|
|
|
|
std::vector<std::string> buf;
|
2022-12-20 15:46:56 -05:00
|
|
|
for(std::string line; std::getline(ifs, line); )
|
|
|
|
{
|
|
|
|
if(line == "")
|
2024-12-02 14:07:39 -05:00
|
|
|
{
|
|
|
|
if(buf.size() == 5)
|
|
|
|
{
|
|
|
|
auto op = ParseOperation(buf[2]);
|
|
|
|
monkeys.push_back(Monkey(//std::atoi(buf[0].substr(7, buf[0].size() - 7).c_str()),
|
|
|
|
ParseItems(buf[1]),
|
|
|
|
op.first,
|
|
|
|
op.second,
|
|
|
|
ParseDivisibleBy(buf[3]),
|
|
|
|
ParseTrue(buf[4]),
|
|
|
|
ParseFalse(buf[5])));
|
|
|
|
buf.clear();
|
|
|
|
}
|
2022-12-20 15:46:56 -05:00
|
|
|
continue;
|
2024-12-02 14:07:39 -05:00
|
|
|
}
|
|
|
|
else if(line.starts_with(" "))
|
|
|
|
buf.push_back(line);
|
2022-12-20 15:46:56 -05:00
|
|
|
}
|
|
|
|
|
2024-12-02 14:07:39 -05:00
|
|
|
for(const auto &monkey : monkeys)
|
|
|
|
std::cout << monkey << std::endl;
|
|
|
|
|
2022-12-20 15:46:56 -05:00
|
|
|
std::cout << " Total: " << total << std::endl;
|
|
|
|
std::cout << "PT2 Total: " << total_pt2 << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-12-02 14:07:39 -05:00
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream &out, const Monkey &monkey)
|
|
|
|
{
|
|
|
|
out << " Starting items: ";
|
|
|
|
for(auto item : monkey.items)
|
|
|
|
out << item << ", ";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> ParseItems(const std::string &line)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::pair<Operation, int> ParseOperation(const std::string &line)
|
|
|
|
{
|
|
|
|
return { Operation::ADD, 0 };
|
|
|
|
}
|
|
|
|
int ParseDivisibleBy(const std::string &line)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int ParseTrue(const std::string &line)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int ParseFalse(const std::string &line)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|