Skip to content

Instantly share code, notes, and snippets.

@kLiHz
Created May 23, 2021 08:56
Show Gist options
  • Save kLiHz/00164392ce3897f4cdc6df6b81e73825 to your computer and use it in GitHub Desktop.
Save kLiHz/00164392ce3897f4cdc6df6b81e73825 to your computer and use it in GitHub Desktop.
USACO21 FEB BRONZE
#include <iostream>
#include <map>
#include <string>
// Assume Cow B in each line appeared before
class Solution {
public:
enum class Relation {BEFORE, AFTER};
enum class Zodiac {
Rat = 1,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
};
Solution() {
birth_year["Bessie"] = 2021;
};
void add(const std::string& a, Relation relation, const std::string& zodiac_name, const std::string & b) {
int gap;
if (relation == Relation::AFTER) {
gap = static_cast<int>(zodiac[zodiac_name]) - static_cast<int>(get_zodiac_for(birth_year[b]));
if (gap <= 0) gap += 12;
birth_year[a] = birth_year[b] + gap;
} else if (relation == Relation::BEFORE) {
gap = static_cast<int>(get_zodiac_for(birth_year[b])) - static_cast<int>(zodiac[zodiac_name]);
if (gap <= 0) gap += 12;
birth_year[a] = birth_year[b] - gap;
}
}
auto get_from_bessie(const std::string& someone) {
return birth_year[someone] - birth_year["Bessie"];
}
private:
static std::map<std::string, Zodiac> zodiac;
std::map<std::string, int> birth_year;
Zodiac get_zodiac_for(int year) {
int gap = year - 2021; // Year of the Ox
while (gap > 10) gap -= 12;
while (gap < -1) gap += 12;
return static_cast<Zodiac>(gap + 2);
}
};
std::map<std::string, Solution::Zodiac> Solution::zodiac = {
{"Rat", static_cast<Zodiac>(1) },
{"Ox", static_cast<Zodiac>(2) },
{"Tiger", static_cast<Zodiac>(3) },
{"Rabbit", static_cast<Zodiac>(4) },
{"Dragon", static_cast<Zodiac>(5) },
{"Snake", static_cast<Zodiac>(6) },
{"Horse", static_cast<Zodiac>(7) },
{"Goat", static_cast<Zodiac>(8) },
{"Monkey", static_cast<Zodiac>(9) },
{"Rooster", static_cast<Zodiac>(10)},
{"Dog", static_cast<Zodiac>(11)},
{"Pig", static_cast<Zodiac>(12)}};
int main() {
Solution inst;
int n;
std::cin >> n;
for (int i = 0; i < n; i++) {
std::string a, tmp_ , relation, animal, from, b;
std::cin >> a >> tmp_ >> tmp_ >> relation >> animal >> tmp_ >> tmp_ >> b;
if (relation == "previous") {
inst.add(a, Solution::Relation::BEFORE, animal, b);
} else if (relation == "next") {
inst.add(a, Solution::Relation::AFTER, animal, b);
}
}
std::cout << std::abs(inst.get_from_bessie("Elsie")) << std::endl;
}
@kLiHz
Copy link
Author

kLiHz commented May 24, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment