Skip to content

Commit 57da347

Browse files
authored
GH-169: Solve codeforces 2A (#187)
1 parent be5d728 commit 57da347

File tree

28 files changed

+300
-30
lines changed

28 files changed

+300
-30
lines changed

collections/codeforces/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</tr>
2323
<tr>
2424
<tr>
25-
<td>2A</td>
25+
<td>🟡&nbsp<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/codeforces2A'>2A</a></td>
2626
<td>2B</td>
2727
<td>2C</td>
2828
<td>2D</td>

collections/codeforces/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ problems:
22
- name: 1A
33
languages: cpp
44
score: 1000
5+
- name: 2A
6+
languages: cpp
7+
score: 1500
58
- name: 4C
69
languages: cpp
710
score: 1300

problems/codeforces2A/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
solution
2+
*.dSYM

problems/codeforces2A/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Codeforces Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run cpp/main:solution < data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <iostream>
4+
#include <map>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <vector>
8+
#include <set>
9+
// #include <fstream>
10+
11+
using namespace std;
12+
13+
int main() {
14+
ios::sync_with_stdio(false);
15+
cin.tie(0);
16+
17+
// bool is_use_file = false;
18+
19+
// filesystem::path filepath =
20+
// filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
21+
// ifstream file(filepath);
22+
23+
// if (is_use_file) {
24+
// cin.rdbuf(file.rdbuf()); // redirect cin to file
25+
// }
26+
27+
int tt;
28+
cin >> tt;
29+
unordered_map<string, int> scores;
30+
vector<pair<string, int>> rounds;
31+
32+
string name;
33+
int score;
34+
35+
while (tt--) {
36+
cin >> name;
37+
cin >> score;
38+
rounds.push_back({name, score});
39+
if (scores.find(name) == scores.end()) {
40+
scores[name] = score;
41+
} else {
42+
scores[name] += score;
43+
}
44+
}
45+
46+
int max_score = INT_MIN;
47+
for (auto it : scores) {
48+
if (it.second > max_score) {
49+
max_score = it.second;
50+
}
51+
}
52+
53+
string winner = "";
54+
55+
// for(auto it: scores){
56+
// cout << it.first << " " << it.second << endl;
57+
// }
58+
59+
set<string> winners;
60+
61+
for (auto it : scores) {
62+
if (it.second == max_score) {
63+
winners.insert(it.first);
64+
}
65+
}
66+
67+
unordered_map<string, int> first_scores;
68+
69+
for(auto it: rounds){
70+
if(winners.find(it.first) != winners.end()){
71+
first_scores[it.first] += it.second;
72+
if(first_scores[it.first] >= max_score){
73+
winner = it.first;
74+
break;
75+
}
76+
}
77+
}
78+
79+
cout << winner << endl;
80+
81+
return 0;
82+
}

tools/craft/templates/codeforces/tests/BUILD renamed to problems/codeforces2A/cpp/tests/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cc_test(
44
name = "solution_test",
55
srcs = ["solution_test.cpp"],
66
deps = [
7-
"//problems/codeforcesAA/src/main:solution",
7+
"//problems/codeforces2A/src/main:solution",
88
"@com_google_googletest//:gtest_main",
99
],
1010
data = ["data"]

tools/craft/templates/codeforces/tests/solution_test.cpp renamed to problems/codeforces2A/cpp/tests/solution_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ string ReadFile(const std::string &filename) {
4646

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

68-
std::string test_data_folder = "problems/codeforcesAA/tests/data";
68+
std::string test_data_folder = "problems/codeforces2A/tests/data";
6969
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
7070
string filename = split(entry.path(), "/").back();
7171
vector<string> v = split(filename, ".");

problems/codeforces2A/data/1.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
mike 3
3+
andrew 5
4+
mike 2

problems/codeforces2A/data/2.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
andrew 3
3+
andrew 2
4+
mike 4

problems/codeforces2A/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
andrew

problems/codeforces2A/data/3.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
kaxqybeultn -352
3+
mgochgrmeyieyskhuourfg -910
4+
kaxqybeultn 691
5+
mgochgrmeyieyskhuourfg -76
6+
kaxqybeultn -303

problems/codeforces2A/data/3.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kaxqybeultn

problems/codeforces2A/data/6.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
15
2+
aawtvezfntstrcpgbzjbf 681
3+
zhahpvqiptvksnbjkdvmknb -74
4+
aawtvezfntstrcpgbzjbf 661
5+
jpdwmyke 474
6+
aawtvezfntstrcpgbzjbf -547
7+
aawtvezfntstrcpgbzjbf 600
8+
zhahpvqiptvksnbjkdvmknb -11
9+
jpdwmyke 711
10+
bjmj 652
11+
aawtvezfntstrcpgbzjbf -1000
12+
aawtvezfntstrcpgbzjbf -171
13+
bjmj -302
14+
aawtvezfntstrcpgbzjbf 961
15+
zhahpvqiptvksnbjkdvmknb 848
16+
bjmj -735

problems/codeforces2A/data/6.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aawtvezfntstrcpgbzjbf

tools/craft/craft.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ def task_rename_files(self):
4545
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
4646

4747
# Update tests/BUILD file
48-
test_build_file = join(problem_dir, "tests/BUILD")
49-
old = "//problems/codeforcesAA/src/main:solution"
50-
new = f"//problems/{domain}{problem_id}/src/main:solution"
48+
test_build_file = join(problem_dir, "cpp/tests/BUILD")
49+
old = "//problems/codeforcesAA/cpp/main:solution"
50+
new = f"//problems/{domain}{problem_id}/cpp/main:solution"
5151
replace_string_in_file(test_build_file, old, new)
5252

5353
# Update tests/solution_test.cpp
54-
test_solution_file = join(problem_dir, "tests/solution_test.cpp")
54+
test_solution_file = join(problem_dir, "cpp/tests/solution_test.cpp")
5555
old = "codeforcesAA"
5656
new = f"{domain}{problem_id}"
5757
replace_string_in_file(test_solution_file, old, new)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
solution
1+
solution
2+
*.dSYM

tools/craft/templates/codeforces/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Run program with an example
66

77
```
8-
bazel run src/main:solution < tests/data/1.in
8+
bazel run cpp/main:solution < data/1.in
99
```
1010

1111
Test program
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <iostream>
4+
#include <map>
5+
#include <set>
6+
#include <string>
7+
#include <unordered_map>
8+
#include <vector>
9+
// #include <fstream>
10+
11+
using namespace std;
12+
13+
int main() {
14+
ios::sync_with_stdio(false);
15+
cin.tie(0);
16+
17+
// filesystem::path filepath =
18+
// filesystem::current_path().parent_path().parent_path() / "data" /
19+
// "1.in";
20+
// ifstream file(filepath);
21+
// cin.rdbuf(file.rdbuf()); // redirect cin to file
22+
23+
int tt;
24+
cin >> tt;
25+
while (tt--) {
26+
cout << tt << '\n';
27+
}
28+
return 0;
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforces2A/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
9+
#include "gtest/gtest.h"
10+
11+
namespace fs = std::filesystem;
12+
13+
using namespace std;
14+
15+
std::string TEST_ID = "";
16+
17+
vector<string> split(string s, string delimiter) {
18+
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
19+
string token;
20+
vector<string> res;
21+
22+
while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
23+
token = s.substr(pos_start, pos_end - pos_start);
24+
pos_start = pos_end + delim_len;
25+
res.push_back(token);
26+
}
27+
28+
res.push_back(s.substr(pos_start));
29+
return res;
30+
}
31+
32+
std::string GetHello(std::string_view in) {
33+
if (in.size() == 0) {
34+
return std::string("hello, word");
35+
} else {
36+
return std::string("Hello, ") + in.data();
37+
}
38+
}
39+
40+
string ReadFile(const std::string &filename) {
41+
std::ifstream f(filename);
42+
string s((std::istreambuf_iterator<char>(f)),
43+
std::istreambuf_iterator<char>());
44+
return s;
45+
}
46+
47+
vector<string> RunProgram(string fileid) {
48+
vector<string> v;
49+
string program = "problems/codeforces2A/src/main/solution";
50+
string test_folder = "problems/codeforces2A/tests/data/";
51+
string command = program + " < " + test_folder + fileid + ".in > output.txt";
52+
std::system(command.c_str());
53+
string actual = ReadFile("output.txt");
54+
string expectedOutputFile = test_folder + fileid + ".out";
55+
string expected = ReadFile(expectedOutputFile);
56+
v.push_back(actual);
57+
v.push_back(expected);
58+
return v;
59+
}
60+
61+
TEST(RunTest, AllTestCases) {
62+
if (TEST_ID != "") {
63+
std::cout << "TEST_ID = " << TEST_ID << std::endl;
64+
} else {
65+
std::cout << "RUN ALL TESTS" << std::endl;
66+
}
67+
68+
std::string test_data_folder = "problems/codeforces2A/tests/data";
69+
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
70+
string filename = split(entry.path(), "/").back();
71+
vector<string> v = split(filename, ".");
72+
string fileid = v[0];
73+
string extension = v[1];
74+
if (extension != "in"){
75+
continue;
76+
}
77+
if (TEST_ID != ""){
78+
if (fileid != TEST_ID){
79+
continue;
80+
}
81+
}
82+
// Run Test Case
83+
vector<string> output = RunProgram(fileid);
84+
string actual = output[0];
85+
string expected = output[1];
86+
string message = "❌ FAIL CASE: " + fileid;
87+
ASSERT_EQ(actual, expected) << message;
88+
}
89+
}
90+
91+
int main(int argc, char **argv) {
92+
::testing::InitGoogleTest(&argc, argv);
93+
if (argc == 2) {
94+
TEST_ID = argv[1];
95+
}
96+
return RUN_ALL_TESTS();
97+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4
2+
3
3+
2
4+
1
5+
0

0 commit comments

Comments
 (0)