Skip to content

GH-96: Create template for AoC #98

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 8 commits into from
Dec 24, 2022
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Each data structures and algorithm has its own separate article with related exp

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

## 📙 Data Structures
## 📙 Data Structures & Algorithms

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.

Expand Down Expand Up @@ -165,6 +165,12 @@ A data structure is a data organization, management, and storage format that is
</tbody>
</table>

## 🔆 Collections

Events of competitive programming

* [Advent of Code 2022](collections/advent-of-code-2022/)

## Contributors

This project exists thanks to all the people who contributed.
Expand Down
29 changes: 26 additions & 3 deletions collections/advent-of-code-2022/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# Collection: Advent of Code 2022
# Advent of Code 2022

Link: https://adventofcode.com/2022/
> 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.

## Problems
> 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.

* Website: https://adventofcode.com/2022/

## Problems

<table>
<thead>
<th></th>
<th>Statement</th>
<th>Part 1</th>
<th>Part 2</th>
</thead>
<tbody>
<tr>
<td>Day 1</td>
<td><a href="https://adventofcode.com/2022/day/1">Link</a></td>
<td>
<a href="../../problems/aoc2022day1a/src/main/solution1.cpp"><code>cpp</code></a>
</td>
<td>
<a href="../../problems/aoc2022day1a/src/main/solution2.cpp"><code>cpp</code></a>
</td>
</tr>
</tbody>
</table>
File renamed without changes.
10 changes: 10 additions & 0 deletions problems/aoc2022day1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Problem

## Usage

Run program with an example

```
bazel run src/main:solution1 `pwd`/data/input.txt
bazel run src/main:solution2 `pwd`/data/input.txt
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solutionA",
srcs = ["solutionA.cpp"],
name = "solution1",
srcs = ["solution1.cpp"],
visibility = ["//visibility:public"]
)

cc_binary(
name = "solutionB",
srcs = ["solutionB.cpp"],
name = "solution2",
srcs = ["solution2.cpp"],
visibility = ["//visibility:public"]
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ int main(int argc, char* argv[]){
string input = argv[1];
string line;
fstream f(input);
long long max_c = 0;
long long sum_c = 0;
vector<long long> v;
while(getline(f, line)){
Expand Down
10 changes: 0 additions & 10 deletions problems/aoc2022day1a/README.md

This file was deleted.

11 changes: 0 additions & 11 deletions problems/aoc2022day1a/tests/BUILD

This file was deleted.

97 changes: 0 additions & 97 deletions problems/aoc2022day1a/tests/solution_test.cpp

This file was deleted.

77 changes: 52 additions & 25 deletions tools/craft/craft.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
PROBLEMS_DIR = join(dirname(dirname(CURRENT_DIR)), "problems")
print(PROBLEMS_DIR)

class CodeforcesGenerator:
pass

def replace_string_in_file(filename, old, new):
with open(filename) as f:
Expand All @@ -17,32 +15,61 @@ def replace_string_in_file(filename, old, new):
with open(filename, "w") as f:
f.write(text)

def task_rename_files(domain, problem_id):
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"
replace_string_in_file(test_build_file, old, new)

# Update tests/solution_test.cpp
test_solution_file = join(problem_dir, "tests/solution_test.cpp")
old = "codeforcesAA"
new = f"{domain}{problem_id}"
replace_string_in_file(test_solution_file, old, new)

class CodeGenerator:
def __init__(self, domain, problem_id):
self.domain = domain
self.problem_id = problem_id

def generate(self):
domain = self.domain
problem_id = self.problem_id
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
try:
shutil.copytree(join(CURRENT_DIR, "templates", domain), problem_dir)
except Exception as e:
print(e)
sys.exit(1)

class CodeforcesGenerator(CodeGenerator):
def __init__(self, domain, problem_id):
super().__init__(domain, problem_id)

def generate(self):
super().generate()
self.task_rename_files()

def task_rename_files(self):
domain = self.domain
problem_id = self.problem_id
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"
replace_string_in_file(test_build_file, old, new)

# Update tests/solution_test.cpp
test_solution_file = join(problem_dir, "tests/solution_test.cpp")
old = "codeforcesAA"
new = f"{domain}{problem_id}"
replace_string_in_file(test_solution_file, old, new)


if __name__ == "__main__":
if len(sys.argv) < 2:
print("Boilderplate to generate workspace for solving problem.")
print("Usage: python crape.py domain problem_id")
print("Boilderplate to generate workspace for solving problem.\n")
print("Usage: python crape.py domain problem_id\n\n")
print("Examples:\n")
print("\tpython crapy.py codeforces 100A\n")
print("\tpython crapy.py aoc 2022day2\n")
sys.exit(1)
domain = sys.argv[1]
problem_id = sys.argv[2]
problem_dir = join(PROBLEMS_DIR, f"{domain}{problem_id}")
try:
shutil.copytree(join(CURRENT_DIR, "templates"), problem_dir)
except Exception as e:
print(e)
sys.exit(1)
task_rename_files(domain, problem_id)
if domain == "codeforces":
GeneratorClass = CodeforcesGenerator
else:
GeneratorClass = CodeGenerator
generator = GeneratorClass(domain, problem_id)
generator.generate()
15 changes: 3 additions & 12 deletions tools/craft/templates/aoc/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
# Problem
# AoC Problem

## Usage

Run program with an example

```
bazel run src/main:solution < tests/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
bazel run src/main:solution1 `pwd`/data/input.txt
bazel run src/main:solution2 `pwd`/data/input.txt
```
1 change: 1 addition & 0 deletions tools/craft/templates/aoc/data/output1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
68787
1 change: 1 addition & 0 deletions tools/craft/templates/aoc/data/output2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
198041
12 changes: 9 additions & 3 deletions tools/craft/templates/aoc/src/main/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solution",
srcs = ["solution.cpp"],
visibility = ["//visibility:public"],
name = "solution1",
srcs = ["solution1.cpp"],
visibility = ["//visibility:public"]
)

cc_binary(
name = "solution2",
srcs = ["solution2.cpp"],
visibility = ["//visibility:public"]
)
19 changes: 0 additions & 19 deletions tools/craft/templates/aoc/src/main/solution.cpp

This file was deleted.

Loading