aboutsummaryrefslogtreecommitdiff
path: root/src/utils.cc
blob: 009754b7a0da1d622c5e8390bc882a054b869bdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "utils.h"
#include "json.hpp"
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <filesystem>
#include <stdexcept>
#include <map>

nlohmann::json utils::loadJson(const std::string& path) {
    std::ifstream f(path);
    nlohmann::json j;
    f >> j;
    return j;
}

void utils::saveJson(const nlohmann::json& data, const std::string& path) {
    std::ofstream f(path);
    f << std::setw(4) << data << std::endl;
}

/*std::string utils::join(std::vector<std::string> parts, std::string joiner) {
    std::stringstream out;
    bool isFirst = true;
    for(std::string p : parts) {
        if(! isFirst) {
            out << joiner;
        }
        isFirst = false;
        out << p;
    }
    return out.str();
}*/

// Accepts coins formatted "X Yp" where X is an integer and Y is any of c, s, e, g, p.
int utils::coins2copper(const std::string& coins) {
    int num = std::stoi(coins.substr(0, coins.find(" ")));
    std::string denomination(coins.substr(coins.find(" ") + 1));
    if(! cpValue.contains(denomination)) {
        throw std::invalid_argument("Unknown coin type: " + denomination);
    }
    return num * cpValue.at(denomination);
}

// Greedily selects highest coin values to minimize total number of coins
// Returns a vector of pairs mapping coin type to coint
std::vector<std::pair<std::string, int>> utils::copper2coins(int coppers) {
    std::vector<std::pair<std::string, int>> ret;
    while(coppers > 0) {
        // Find the largest denomination in cpValue under coppers
        std::pair<std::string, int> largest("", 0);
        for(auto pair : cpValue) {
            if(pair.second <= coppers && pair.second > largest.second) {
                largest = pair;
            }
        }
        std::pair<std::string, int> amnt(largest.first, coppers / largest.second);
        coppers -= amnt.second * largest.second;
        ret.push_back(amnt);
    }
    return ret;
}

std::string utils::getCostString(int coppers) {
    std::vector<std::string> parts;
    for(auto pair : utils::copper2coins(coppers)) {
        parts.push_back(std::to_string(pair.second) + " " + pair.first);
    }
    return utils::join(parts, ", ");
}