What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
In the board game 7 Wonders, there are four basic resources, which we'll abbreviate with letters: W for wood, B for brick, S for stone, and O for ore.
Resource cards let you produce one of your choice of resources. We'll use W/B to represent a resource card that can give you either 1 wood or 1 brick, but not both.
Given the resource cards W/B/S/O, W, S/B, and S, it is possible for you to produce 2 wood and 2 stone: use the first two cards to get wood, and the last two to get stone. However, with that same set of cards, it is impossible for you to produce 2 wood and 2 brick.
Input
You'll be given a comma-separated sequence of cards inside of square brackets, with the features separated by a slash. Your target will be given as "Can you make ____?" with the list of resources to target, one per card.
Note: in the game 7 Wonders, every card produces either 1, 2, or all 4 of the resources. But if you want a challenge, make your program support any number of resources instead of just W,B,S,O, and make your program accept arbitrary resource cards.
Output
Whether it is possible to generate the desired resources, and if so, how.
Example Inputs
With line breaks for clarity.
Cards [W/B/S/O, W, S/B, S]. Can you make WWSS?
Cards [W/B/S/O, S/O, W/S, W/B, W/B, W, B]. Can you make WWBSSOO?
Cards [A/B/D/E, A/B/E/F/G, A/D, A/D/E, A/D/E, B/C/D/G, B/C/E, B/C/E/F,
B/C/E/F, B/D/E, B/D/E, B/E/F, C/D/F, C/E, C/E/F/G, C/F, C/F, D/E/F/G,
D/F, E/G]. Can you make AABCCCCCCDDDEEEEFFGG?
Cards [A/C/G/K/L/O/R/S, A/D/H/I/M/Q, A/D/K/W/X, A/D/M/U/Z, A/E/J/M/T,
A/G/H/I/M/R/T/Z, A/G/M/T/U, A/H/I/J/Q, B/C/Q/U/V, B/D/F/K/M/R/W/Y,
B/F/P/T/U/W/Y, B/G/K/M/S/T/X/Y, C/E/F/I/K/N/O, D/E/G/J/M/Q/Z, D/G/I/R/Z,
D/H/I/T/U, E/G/H/J/M/Q, E/G/H/J/Q/R/T/U, E/G/J/M/Z, E/H/I/Q/T/U/Z,
E/J/O/S/V/X, F/G/H/N/P/V, F/G/N/P/R/S/Z, F/I/M/Q/R/U/Z, F/L/M/P/S/V/W/Y,
G/H/J/M/Q]. Can you make ABCDEFGHIJKLMNOPQRSTUVWXYZ?
Solution
in C++
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
class Card {
static int id_counter;
public:
int id;
std::string resources;
char usedFor = '-';
Card(std::string str, std::string alphabet) {
id = ++id_counter;
// Purge "/"
str.erase(std::remove(str.begin(), str.end(), '/'), str.end());
// Purge options not in the alphabet
for (auto c : str)
if (alphabet.find(c) != -1)
resources += c;
}
};
bool compareCardsLen(Card a, Card b) { return (a.resources.length() < b.resources.length()); }
bool compareCardsId(Card a, Card b) { return (a.id < b.id); }
bool solve(std::vector<Card> cards, std::string searched, int index = 0) {
if (searched.empty()) {
std::sort(cards.begin(), cards.end(), compareCardsId);
for (auto card : cards)
std::cout << "card " << card.id << " - " << card.resources << " - " << card.usedFor << std::endl;
return true;
}
auto& card = cards.at(index);
for (auto c : card.resources) {
int pos = searched.find(c);
if (pos != -1) {
card.usedFor = c;
if (solve(cards, searched.substr(0, pos) + searched.substr(pos + 1), index + 1))
return true;
}
}
return false;
}
int Card::id_counter = 0;
int main()
{
// Read data
std::string input;
std::getline(std::cin, input);
// Process what is searched
std::size_t searchedStart = input.find("Can you make ") + 13;
std::string searched = input.substr(searchedStart, input.length() - searchedStart - 1);
// Process alphabet
std::string alphabet;
for (auto c : searched) {
if (alphabet.find(c) == -1)
alphabet += c;
}
// Process cards
std::size_t rawStart = input.find("[") + 1;
std::size_t rawEnd = input.find("]");
std::string cardsRaw = input.substr(rawStart, rawEnd - rawStart);
std::vector<Card> cards;
int commaPos = -1;
do {
commaPos = cardsRaw.find(",");
std::string card = cardsRaw.substr(0, commaPos);
cards.push_back(Card(card, alphabet));
cardsRaw = cardsRaw.substr(commaPos + 2);
} while (commaPos != -1);
std::sort(cards.begin(), cards.end(), compareCardsLen);
// Check if possible (optimized brute force)
if (!solve(cards, searched))
std::cout << "No solution possible!" << std::endl;
return 0;
}
In the board game 7 Wonders, there are four basic resources, which we'll abbreviate with letters: W for wood, B for brick, S for stone, and O for ore.
Resource cards let you produce one of your choice of resources. We'll use W/B to represent a resource card that can give you either 1 wood or 1 brick, but not both.
Given the resource cards W/B/S/O, W, S/B, and S, it is possible for you to produce 2 wood and 2 stone: use the first two cards to get wood, and the last two to get stone. However, with that same set of cards, it is impossible for you to produce 2 wood and 2 brick.
Input
You'll be given a comma-separated sequence of cards inside of square brackets, with the features separated by a slash. Your target will be given as "Can you make ____?" with the list of resources to target, one per card.
Note: in the game 7 Wonders, every card produces either 1, 2, or all 4 of the resources. But if you want a challenge, make your program support any number of resources instead of just W,B,S,O, and make your program accept arbitrary resource cards.
Output
Whether it is possible to generate the desired resources, and if so, how.
Example Inputs
With line breaks for clarity.
Cards [W/B/S/O, W, S/B, S]. Can you make WWSS?
Cards [W/B/S/O, S/O, W/S, W/B, W/B, W, B]. Can you make WWBSSOO?
Cards [A/B/D/E, A/B/E/F/G, A/D, A/D/E, A/D/E, B/C/D/G, B/C/E, B/C/E/F,
B/C/E/F, B/D/E, B/D/E, B/E/F, C/D/F, C/E, C/E/F/G, C/F, C/F, D/E/F/G,
D/F, E/G]. Can you make AABCCCCCCDDDEEEEFFGG?
Cards [A/C/G/K/L/O/R/S, A/D/H/I/M/Q, A/D/K/W/X, A/D/M/U/Z, A/E/J/M/T,
A/G/H/I/M/R/T/Z, A/G/M/T/U, A/H/I/J/Q, B/C/Q/U/V, B/D/F/K/M/R/W/Y,
B/F/P/T/U/W/Y, B/G/K/M/S/T/X/Y, C/E/F/I/K/N/O, D/E/G/J/M/Q/Z, D/G/I/R/Z,
D/H/I/T/U, E/G/H/J/M/Q, E/G/H/J/Q/R/T/U, E/G/J/M/Z, E/H/I/Q/T/U/Z,
E/J/O/S/V/X, F/G/H/N/P/V, F/G/N/P/R/S/Z, F/I/M/Q/R/U/Z, F/L/M/P/S/V/W/Y,
G/H/J/M/Q]. Can you make ABCDEFGHIJKLMNOPQRSTUVWXYZ?
Solution
in C++
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
class Card {
static int id_counter;
public:
int id;
std::string resources;
char usedFor = '-';
Card(std::string str, std::string alphabet) {
id = ++id_counter;
// Purge "/"
str.erase(std::remove(str.begin(), str.end(), '/'), str.end());
// Purge options not in the alphabet
for (auto c : str)
if (alphabet.find(c) != -1)
resources += c;
}
};
bool compareCardsLen(Card a, Card b) { return (a.resources.length() < b.resources.length()); }
bool compareCardsId(Card a, Card b) { return (a.id < b.id); }
bool solve(std::vector<Card> cards, std::string searched, int index = 0) {
if (searched.empty()) {
std::sort(cards.begin(), cards.end(), compareCardsId);
for (auto card : cards)
std::cout << "card " << card.id << " - " << card.resources << " - " << card.usedFor << std::endl;
return true;
}
auto& card = cards.at(index);
for (auto c : card.resources) {
int pos = searched.find(c);
if (pos != -1) {
card.usedFor = c;
if (solve(cards, searched.substr(0, pos) + searched.substr(pos + 1), index + 1))
return true;
}
}
return false;
}
int Card::id_counter = 0;
int main()
{
// Read data
std::string input;
std::getline(std::cin, input);
// Process what is searched
std::size_t searchedStart = input.find("Can you make ") + 13;
std::string searched = input.substr(searchedStart, input.length() - searchedStart - 1);
// Process alphabet
std::string alphabet;
for (auto c : searched) {
if (alphabet.find(c) == -1)
alphabet += c;
}
// Process cards
std::size_t rawStart = input.find("[") + 1;
std::size_t rawEnd = input.find("]");
std::string cardsRaw = input.substr(rawStart, rawEnd - rawStart);
std::vector<Card> cards;
int commaPos = -1;
do {
commaPos = cardsRaw.find(",");
std::string card = cardsRaw.substr(0, commaPos);
cards.push_back(Card(card, alphabet));
cardsRaw = cardsRaw.substr(commaPos + 2);
} while (commaPos != -1);
std::sort(cards.begin(), cards.end(), compareCardsLen);
// Check if possible (optimized brute force)
if (!solve(cards, searched))
std::cout << "No solution possible!" << std::endl;
return 0;
}
Comments
Post a Comment