Challenge Starter (public template)
December 1, 2022 ยท View on GitHub
Template project designed to host algorithm solutions for a variety of coding challenges (think Advent of Code, HackerRank, Project Euler, and similar coding challenge websites).
Main Advantages and Features
- Consolidate all your Coding Challenge-based algorithms under a single Repository
- Easily test your proposed solutions to see if they measure up as solutions
- Concentrate on what matters most: checking out if your proposed algorithms work and just going from one problem to the next!
Check It Out!
For a live-action Repository and see how this all spans out, check out my Code Challenges Repository!
What's Under the Hood?
You can check out Challenge Base, the package on which this project relies, for more insights on what's happening under the hood.
How Will You Get Started?
Once you begin to grasp how to interact with all the different file types there are, you'll get up and running in little to no time. So, here's how you get started:
For this walkthrough, I'll be using the Advent of Code 2021 as an example, so make sure to be mildly familiarized with how it works! This codebase already contains some sample code answering both Questions for Advent of Code 2021 - Day 01, so make sure to read those up to get a bit more understanding as we go along each step!
Essentially, within Advent of Code Challenges, each day in the Advent Calendar will pose two different Questions or Problems, with each potentially requiring related algorithms to solve both problems.
NOTE: This codebase has been built on Xcode 14.1, so expect to run any code within Xcode itself.
-
Start by creating a new folder under
Resourcesand underChallengesto label the new Coding Challenge you'll be undertaking. Make sure to name both of these folders the same!- For our walkthrough, we would then create two folders: one with the path for
ChallengeStarter/Challenges/SampleChallengeand another one with the path forChallengeStarter/Resources/SampleChallenge. - The
Resourcesfolder will contain all input and output data in their dedicated files. - The
Challengesfolder will contain all the algorithms needed to perform whatever is necessary to calculate some output data from the given input file(s).
- For our walkthrough, we would then create two folders: one with the path for
-
Once you know what Challenge you'll be performing for, you may go ahead and create a new Challenge file under the
Challengessubfolder you just created. Ideally, you should name this Challenge file with the same name of the folder it is in, as all Solutions will inherit from this class to more easily identify to which Challenge Set a specific Solution belongs to.- For our walkthrough, this file would be
ChallengeStarter/Challenges/SampleChallenge/SampleChallenge.swift. - After creating this file, you would only need to configure the
Algorithmsenum, specifying the names and amounts of algorithms that you desire to create for each Solution. - In the case of Advent of Code Challenges, since each day has two problems (or parts), I have opted to think of each part as an algorithm of its own. Thus, the
Algorithmsenum can stay as it is defined by the template file.
- For our walkthrough, this file would be
-
Since Challenge Problems usually contain some sample data along with its expected output, you can add any and all resource files under the
Resourcesfolder, inside of the subfolder you created in the previous step.- For our walkthrough, these files are located under the
ChallengeStarter/Resources/SampleChallengefolder. - Sample data (examples discussed as part of a given problem) are considered as
Test Cases. Each of theseTest Casesshould contain their correspondinginputandoutputfiles, with the following naming structure:- Input data files must have a
.inextension. - Output data files must have a
.outextension. - Both the Input and Output data files must have the same name (without the extension) in order to be considered as a single
Test Case. - Each
Test Casemust be named as:SolutionName[.ResourceName][.AlgorithmName].<in|out>SolutionNamewould represent what specific Problem would this sample data belong to. Make it something useful and straightforward for you.[.ResourceName]is optional, and it essentially represents what scenario you are testing for. Please enter this portion without square brackets.[.AlgorithmName]is also optional, and it should be the same as anyAlgorithmthat was defined in the enum from the previous step.<in|out>specifies whether the data in the file should be interpreted as input (inextension) or expected output (outextension) data. Both of these files should exist in order for them to be considered a validTest Case.
- Input data files must have a
- Actual data (usually larger data files which must be computed to produce a specific solution that is to be used as your response within the website) are considered as
Actual Data. UnlikeTest Cases, these files should only contain a single.infile (with no corresponding.outfile).
- For our walkthrough, these files are located under the
-
Once you have configured and added any
Test CasesandActual Data, you can go ahead and create a Solution file. This file contains multiple sections that will be broken down below, specifying what needs to change in each of them.- For our walkthrough, this file would be
ChallengeStarter/Challenges/SampleChallenge/Solution00.swift. - Out of the box, you would typically need to modify the
typealiasvariables at the very top of the class and both theassembleandactivatemethods.- For the
typealiasvariables, think of a data structure that would best suit this specific problem. What would it be? Based on this answer, you can specify theInputandOutputtypealiasesto ease your algorithm logic. - Within the
assemblemethod, you would make any necessary changes to it so that theTest Caseinput and output files would be interpreted and transformed onto the data structures that you elaborated on the bullet above. - The
activatemethod only needs changes whenever theAlgorithmsenum (covered in Step #2) changes. AllAlgorithmscases should be covered and mapped accordingly. - The
Logic Methodssection would, as you have probably guessed by now, contain the specific algorithms you'll be creating to come up with a Solution to the problem poised by the Challenge website. Go on, strive to make a clever Solution!
- For the
- For our walkthrough, this file would be
-
Now for probably the most exciting part: executing your code to see if it works! The
ChallengeStarter/main.swiftfile already contains some lines to execute our walkthrough code.- Expect to change this with the appropriate
ChallengeandSolutionnames for your own Solutions. - For each
Solution, you may feed it with...- An array containing
resourceSetsto be used, if any. These were defined earlier in Step #3 asResourceName. If noTest Caseswere provided (or an emptyStringarray is provided), validating logic is skipped and executing the program goes directly to executing theActual Data. - An array containing the set of
Algorithmsto be used for the current execution. These were defined above in Step #2 and are defined in the Challenge file. If noAlgorithmswere provided (or an emptyAlgorithmsarray is provided), all definedAlgorithmsare executed subsequently.
- An array containing
- NOTE: If at least one
Test Casefails, execution ofActual Datais not performed.
- Expect to change this with the appropriate
-
By this step, you should have a fair grasp of how to utilize this template project and continue to build your own awesome solutions!
- Even so, I reckon it might have its own learning curve to understand well what's going on and how to move forward. Don't be discouraged! You can check out my Code Challenges Repository to get yourself acquainted with a practical usage for this project and how to get around.
Want to Get Started Even Faster?
You can download my Challenge Template Files, which contains the set of files that can be installed for Xcode to get a head start on creating all the necessary files.
Feel Like Contributing?
Contribution is by all means encouraged and welcome! You can take a look at the following Repositories and make your contributions, whether big or small, and keep this project moving forward!
- Challenge Base Project, the Package behind this whole initiative to streamline Coding Challenges, reducing boilerplate code and logic.
- Challenge Template Files, provides various Swift Template files that can be used along with this Project.
- Challenge Starter Project (this Repo), any improvements to streamline the Challenge setup is greatly welcome.