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).
- 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!
For a live-action Repository and see how this all spans out, check out my Code Challenges Repository!
You can check out Challenge Base, the package on which this project relies, for more insights on what's happening under the hood.
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
Resources
and underChallenges
to 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/SampleChallenge
and another one with the path forChallengeStarter/Resources/SampleChallenge
. - The
Resources
folder will contain all input and output data in their dedicated files. - The
Challenges
folder 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
Challenges
subfolder 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
Algorithms
enum, 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
Algorithms
enum 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
Resources
folder, inside of the subfolder you created in the previous step.- For our walkthrough, these files are located under the
ChallengeStarter/Resources/SampleChallenge
folder. - Sample data (examples discussed as part of a given problem) are considered as
Test Cases
. Each of theseTest Cases
should contain their correspondinginput
andoutput
files, with the following naming structure:- Input data files must have a
.in
extension. - Output data files must have a
.out
extension. - 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 Case
must be named as:SolutionName[.ResourceName][.AlgorithmName].<in|out>
SolutionName
would 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 anyAlgorithm
that was defined in the enum from the previous step.<in|out>
specifies whether the data in the file should be interpreted as input (in
extension) or expected output (out
extension) 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.in
file (with no corresponding.out
file).
- For our walkthrough, these files are located under the
-
Once you have configured and added any
Test Cases
andActual 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
typealias
variables at the very top of the class and both theassemble
andactivate
methods.- For the
typealias
variables, think of a data structure that would best suit this specific problem. What would it be? Based on this answer, you can specify theInput
andOutput
typealiases
to ease your algorithm logic. - Within the
assemble
method, you would make any necessary changes to it so that theTest Case
input and output files would be interpreted and transformed onto the data structures that you elaborated on the bullet above. - The
activate
method only needs changes whenever theAlgorithms
enum (covered in Step #2) changes. AllAlgorithms
cases should be covered and mapped accordingly. - The
Logic Methods
section 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.swift
file already contains some lines to execute our walkthrough code.- Expect to change this with the appropriate
Challenge
andSolution
names for your own Solutions. - For each
Solution
, you may feed it with...- An array containing
resourceSets
to be used, if any. These were defined earlier in Step #3 asResourceName
. If noTest Cases
were provided (or an emptyString
array is provided), validating logic is skipped and executing the program goes directly to executing theActual Data
. - An array containing the set of
Algorithms
to be used for the current execution. These were defined above in Step #2 and are defined in the Challenge file. If noAlgorithms
were provided (or an emptyAlgorithms
array is provided), all definedAlgorithms
are executed subsequently.
- An array containing
- NOTE: If at least one
Test Case
fails, execution ofActual Data
is 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.
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.
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.