Skip to content

GH-169: Solve codeforces 2A #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collections/codeforces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</tr>
<tr>
<tr>
<td>2A</td>
<td>🟡&nbsp<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/codeforces2A'>2A</a></td>
<td>2B</td>
<td>2C</td>
<td>2D</td>
Expand Down
3 changes: 3 additions & 0 deletions collections/codeforces/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ problems:
- name: 1A
languages: cpp
score: 1000
- name: 2A
languages: cpp
score: 1500
- name: 4C
languages: cpp
score: 1300
Expand Down
2 changes: 2 additions & 0 deletions problems/codeforces2A/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
solution
*.dSYM
19 changes: 19 additions & 0 deletions problems/codeforces2A/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Codeforces Problem

## Usage

Run program with an example

```
bazel run cpp/main:solution < data/1.in
```

Test program

```
# Run all tests
bazel test --test_output=all tests:solution_test
bazel test --test_output=all --cache_test_results=no tests:solution_test
# Run test with pecific test id
bazel test --test_output=all tests:solution_test --test_arg=1
```
82 changes: 82 additions & 0 deletions problems/codeforces2A/cpp/main/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
#include <set>
// #include <fstream>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);

// bool is_use_file = false;

// filesystem::path filepath =
// filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
// ifstream file(filepath);

// if (is_use_file) {
// cin.rdbuf(file.rdbuf()); // redirect cin to file
// }

int tt;
cin >> tt;
unordered_map<string, int> scores;
vector<pair<string, int>> rounds;

string name;
int score;

while (tt--) {
cin >> name;
cin >> score;
rounds.push_back({name, score});
if (scores.find(name) == scores.end()) {
scores[name] = score;
} else {
scores[name] += score;
}
}

int max_score = INT_MIN;
for (auto it : scores) {
if (it.second > max_score) {
max_score = it.second;
}
}

string winner = "";

// for(auto it: scores){
// cout << it.first << " " << it.second << endl;
// }

set<string> winners;

for (auto it : scores) {
if (it.second == max_score) {
winners.insert(it.first);
}
}

unordered_map<string, int> first_scores;

for(auto it: rounds){
if(winners.find(it.first) != winners.end()){
first_scores[it.first] += it.second;
if(first_scores[it.first] >= max_score){
winner = it.first;
break;
}
}
}

cout << winner << endl;

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cc_test(
name = "solution_test",
srcs = ["solution_test.cpp"],
deps = [
"//problems/codeforcesAA/src/main:solution",
"//problems/codeforces2A/src/main:solution",
"@com_google_googletest//:gtest_main",
],
data = ["data"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ string ReadFile(const std::string &filename) {

vector<string> RunProgram(string fileid) {
vector<string> v;
string program = "problems/codeforcesAA/src/main/solution";
string test_folder = "problems/codeforcesAA/tests/data/";
string program = "problems/codeforces2A/src/main/solution";
string test_folder = "problems/codeforces2A/tests/data/";
string command = program + " < " + test_folder + fileid + ".in > output.txt";
std::system(command.c_str());
string actual = ReadFile("output.txt");
Expand All @@ -65,7 +65,7 @@ TEST(RunTest, AllTestCases) {
std::cout << "RUN ALL TESTS" << std::endl;
}

std::string test_data_folder = "problems/codeforcesAA/tests/data";
std::string test_data_folder = "problems/codeforces2A/tests/data";
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
string filename = split(entry.path(), "/").back();
vector<string> v = split(filename, ".");
Expand Down
4 changes: 4 additions & 0 deletions problems/codeforces2A/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
mike 3
andrew 5
mike 2
4 changes: 4 additions & 0 deletions problems/codeforces2A/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
andrew 3
andrew 2
mike 4
1 change: 1 addition & 0 deletions problems/codeforces2A/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
andrew
6 changes: 6 additions & 0 deletions problems/codeforces2A/data/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5
kaxqybeultn -352
mgochgrmeyieyskhuourfg -910
kaxqybeultn 691
mgochgrmeyieyskhuourfg -76
kaxqybeultn -303
1 change: 1 addition & 0 deletions problems/codeforces2A/data/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kaxqybeultn
16 changes: 16 additions & 0 deletions problems/codeforces2A/data/6.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
15
aawtvezfntstrcpgbzjbf 681
zhahpvqiptvksnbjkdvmknb -74
aawtvezfntstrcpgbzjbf 661
jpdwmyke 474
aawtvezfntstrcpgbzjbf -547
aawtvezfntstrcpgbzjbf 600
zhahpvqiptvksnbjkdvmknb -11
jpdwmyke 711
bjmj 652
aawtvezfntstrcpgbzjbf -1000
aawtvezfntstrcpgbzjbf -171
bjmj -302
aawtvezfntstrcpgbzjbf 961
zhahpvqiptvksnbjkdvmknb 848
bjmj -735
1 change: 1 addition & 0 deletions problems/codeforces2A/data/6.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aawtvezfntstrcpgbzjbf
8 changes: 4 additions & 4 deletions tools/craft/craft.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ def task_rename_files(self):
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")

# Update tests/BUILD file
test_build_file = join(problem_dir, "tests/BUILD")
old = "//problems/codeforcesAA/src/main:solution"
new = f"//problems/{domain}{problem_id}/src/main:solution"
test_build_file = join(problem_dir, "cpp/tests/BUILD")
old = "//problems/codeforcesAA/cpp/main:solution"
new = f"//problems/{domain}{problem_id}/cpp/main:solution"
replace_string_in_file(test_build_file, old, new)

# Update tests/solution_test.cpp
test_solution_file = join(problem_dir, "tests/solution_test.cpp")
test_solution_file = join(problem_dir, "cpp/tests/solution_test.cpp")
old = "codeforcesAA"
new = f"{domain}{problem_id}"
replace_string_in_file(test_solution_file, old, new)
Expand Down
3 changes: 2 additions & 1 deletion tools/craft/templates/codeforces/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
solution
solution
*.dSYM
2 changes: 1 addition & 1 deletion tools/craft/templates/codeforces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Run program with an example

```
bazel run src/main:solution < tests/data/1.in
bazel run cpp/main:solution < data/1.in
```

Test program
Expand Down
7 changes: 7 additions & 0 deletions tools/craft/templates/codeforces/cpp/main/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solution",
srcs = ["solution.cpp"],
visibility = ["//visibility:public"],
)
29 changes: 29 additions & 0 deletions tools/craft/templates/codeforces/cpp/main/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #include <fstream>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);

// filesystem::path filepath =
// filesystem::current_path().parent_path().parent_path() / "data" /
// "1.in";
// ifstream file(filepath);
// cin.rdbuf(file.rdbuf()); // redirect cin to file

int tt;
cin >> tt;
while (tt--) {
cout << tt << '\n';
}
return 0;
}
11 changes: 11 additions & 0 deletions tools/craft/templates/codeforces/cpp/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")

cc_test(
name = "solution_test",
srcs = ["solution_test.cpp"],
deps = [
"//problems/codeforces2A/src/main:solution",
"@com_google_googletest//:gtest_main",
],
data = ["data"]
)
97 changes: 97 additions & 0 deletions tools/craft/templates/codeforces/cpp/tests/solution_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

#include "gtest/gtest.h"

namespace fs = std::filesystem;

using namespace std;

std::string TEST_ID = "";

vector<string> split(string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;

while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}

res.push_back(s.substr(pos_start));
return res;
}

std::string GetHello(std::string_view in) {
if (in.size() == 0) {
return std::string("hello, word");
} else {
return std::string("Hello, ") + in.data();
}
}

string ReadFile(const std::string &filename) {
std::ifstream f(filename);
string s((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
return s;
}

vector<string> RunProgram(string fileid) {
vector<string> v;
string program = "problems/codeforces2A/src/main/solution";
string test_folder = "problems/codeforces2A/tests/data/";
string command = program + " < " + test_folder + fileid + ".in > output.txt";
std::system(command.c_str());
string actual = ReadFile("output.txt");
string expectedOutputFile = test_folder + fileid + ".out";
string expected = ReadFile(expectedOutputFile);
v.push_back(actual);
v.push_back(expected);
return v;
}

TEST(RunTest, AllTestCases) {
if (TEST_ID != "") {
std::cout << "TEST_ID = " << TEST_ID << std::endl;
} else {
std::cout << "RUN ALL TESTS" << std::endl;
}

std::string test_data_folder = "problems/codeforces2A/tests/data";
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
string filename = split(entry.path(), "/").back();
vector<string> v = split(filename, ".");
string fileid = v[0];
string extension = v[1];
if (extension != "in"){
continue;
}
if (TEST_ID != ""){
if (fileid != TEST_ID){
continue;
}
}
// Run Test Case
vector<string> output = RunProgram(fileid);
string actual = output[0];
string expected = output[1];
string message = "❌ FAIL CASE: " + fileid;
ASSERT_EQ(actual, expected) << message;
}
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
if (argc == 2) {
TEST_ID = argv[1];
}
return RUN_ALL_TESTS();
}
5 changes: 5 additions & 0 deletions tools/craft/templates/codeforces/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4
3
2
1
0
Loading