Skip to content

Commit ed85979

Browse files
authored
GH-96: Create template for AoC (#98)
1 parent 509b85c commit ed85979

File tree

29 files changed

+170
-312
lines changed

29 files changed

+170
-312
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Each data structures and algorithm has its own separate article with related exp
66

77
[New Version 2.2 is released.](https://github.com/rain1024/datastructures-algorithms-competitive-programming/releases)
88

9-
## 📙 Data Structures
9+
## 📙 Data Structures & Algorithms
1010

1111
A data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More recisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.
1212

@@ -165,6 +165,12 @@ A data structure is a data organization, management, and storage format that is
165165
</tbody>
166166
</table>
167167

168+
## 🔆 Collections
169+
170+
Events of competitive programming
171+
172+
* [Advent of Code 2022](collections/advent-of-code-2022/)
173+
168174
## Contributors
169175

170176
This project exists thanks to all the people who contributed.
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
# Collection: Advent of Code 2022
1+
# Advent of Code 2022
22

3-
Link: https://adventofcode.com/2022/
3+
> Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.
44
5-
## Problems
5+
> You don't need a computer science background to participate - just a little programming knowledge and some problem solving skills will get you pretty far. Nor do you need a fancy computer; every problem has a solution that completes in at most 15 seconds on ten-year-old hardware.
6+
7+
* Website: https://adventofcode.com/2022/
68

9+
## Problems
710

11+
<table>
12+
<thead>
13+
<th></th>
14+
<th>Statement</th>
15+
<th>Part 1</th>
16+
<th>Part 2</th>
17+
</thead>
18+
<tbody>
19+
<tr>
20+
<td>Day 1</td>
21+
<td><a href="https://adventofcode.com/2022/day/1">Link</a></td>
22+
<td>
23+
<a href="../../problems/aoc2022day1a/src/main/solution1.cpp"><code>cpp</code></a>
24+
</td>
25+
<td>
26+
<a href="../../problems/aoc2022day1a/src/main/solution2.cpp"><code>cpp</code></a>
27+
</td>
28+
</tr>
29+
</tbody>
30+
</table>
File renamed without changes.

problems/aoc2022day1/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run src/main:solution1 `pwd`/data/input.txt
9+
bazel run src/main:solution2 `pwd`/data/input.txt
10+
```
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
22

33
cc_binary(
4-
name = "solutionA",
5-
srcs = ["solutionA.cpp"],
4+
name = "solution1",
5+
srcs = ["solution1.cpp"],
66
visibility = ["//visibility:public"]
77
)
88

99
cc_binary(
10-
name = "solutionB",
11-
srcs = ["solutionB.cpp"],
10+
name = "solution2",
11+
srcs = ["solution2.cpp"],
1212
visibility = ["//visibility:public"]
1313
)

problems/aoc2022day1a/src/main/solutionB.cpp renamed to problems/aoc2022day1/src/main/solution2.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ int main(int argc, char* argv[]){
1313
string input = argv[1];
1414
string line;
1515
fstream f(input);
16-
long long max_c = 0;
1716
long long sum_c = 0;
1817
vector<long long> v;
1918
while(getline(f, line)){

problems/aoc2022day1a/README.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

problems/aoc2022day1a/tests/BUILD

Lines changed: 0 additions & 11 deletions
This file was deleted.

problems/aoc2022day1a/tests/solution_test.cpp

Lines changed: 0 additions & 97 deletions
This file was deleted.

tools/craft/craft.py

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
PROBLEMS_DIR = join(dirname(dirname(CURRENT_DIR)), "problems")
88
print(PROBLEMS_DIR)
99

10-
class CodeforcesGenerator:
11-
pass
1210

1311
def replace_string_in_file(filename, old, new):
1412
with open(filename) as f:
@@ -17,32 +15,61 @@ def replace_string_in_file(filename, old, new):
1715
with open(filename, "w") as f:
1816
f.write(text)
1917

20-
def task_rename_files(domain, problem_id):
21-
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
22-
23-
# Update tests/BUILD file
24-
test_build_file = join(problem_dir, "tests/BUILD")
25-
old = "//problems/codeforcesAA/src/main:solution"
26-
new = f"//problems/{domain}{problem_id}/src/main:solution"
27-
replace_string_in_file(test_build_file, old, new)
28-
29-
# Update tests/solution_test.cpp
30-
test_solution_file = join(problem_dir, "tests/solution_test.cpp")
31-
old = "codeforcesAA"
32-
new = f"{domain}{problem_id}"
33-
replace_string_in_file(test_solution_file, old, new)
18+
19+
class CodeGenerator:
20+
def __init__(self, domain, problem_id):
21+
self.domain = domain
22+
self.problem_id = problem_id
23+
24+
def generate(self):
25+
domain = self.domain
26+
problem_id = self.problem_id
27+
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
28+
try:
29+
shutil.copytree(join(CURRENT_DIR, "templates", domain), problem_dir)
30+
except Exception as e:
31+
print(e)
32+
sys.exit(1)
33+
34+
class CodeforcesGenerator(CodeGenerator):
35+
def __init__(self, domain, problem_id):
36+
super().__init__(domain, problem_id)
37+
38+
def generate(self):
39+
super().generate()
40+
self.task_rename_files()
41+
42+
def task_rename_files(self):
43+
domain = self.domain
44+
problem_id = self.problem_id
45+
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
46+
47+
# 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"
51+
replace_string_in_file(test_build_file, old, new)
52+
53+
# Update tests/solution_test.cpp
54+
test_solution_file = join(problem_dir, "tests/solution_test.cpp")
55+
old = "codeforcesAA"
56+
new = f"{domain}{problem_id}"
57+
replace_string_in_file(test_solution_file, old, new)
58+
3459

3560
if __name__ == "__main__":
3661
if len(sys.argv) < 2:
37-
print("Boilderplate to generate workspace for solving problem.")
38-
print("Usage: python crape.py domain problem_id")
62+
print("Boilderplate to generate workspace for solving problem.\n")
63+
print("Usage: python crape.py domain problem_id\n\n")
64+
print("Examples:\n")
65+
print("\tpython crapy.py codeforces 100A\n")
66+
print("\tpython crapy.py aoc 2022day2\n")
3967
sys.exit(1)
4068
domain = sys.argv[1]
4169
problem_id = sys.argv[2]
42-
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
43-
try:
44-
shutil.copytree(join(CURRENT_DIR, "templates"), problem_dir)
45-
except Exception as e:
46-
print(e)
47-
sys.exit(1)
48-
task_rename_files(domain, problem_id)
70+
if domain == "codeforces":
71+
GeneratorClass = CodeforcesGenerator
72+
else:
73+
GeneratorClass = CodeGenerator
74+
generator = GeneratorClass(domain, problem_id)
75+
generator.generate()

tools/craft/templates/aoc/README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
# Problem
1+
# AoC Problem
22

33
## Usage
44

55
Run program with an example
66

77
```
8-
bazel run src/main:solution < tests/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
8+
bazel run src/main:solution1 `pwd`/data/input.txt
9+
bazel run src/main:solution2 `pwd`/data/input.txt
1910
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
68787
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
198041
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
22

33
cc_binary(
4-
name = "solution",
5-
srcs = ["solution.cpp"],
6-
visibility = ["//visibility:public"],
4+
name = "solution1",
5+
srcs = ["solution1.cpp"],
6+
visibility = ["//visibility:public"]
7+
)
8+
9+
cc_binary(
10+
name = "solution2",
11+
srcs = ["solution2.cpp"],
12+
visibility = ["//visibility:public"]
713
)

tools/craft/templates/aoc/src/main/solution.cpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)