Package: aoc-core
aoc-coreAdvent of Code in Python.InstallingPython 3.8 or above is required.pipInstalling the library withpipis quite simple:$pipinstallaoc-coreAlternatively, the library can be installed from source:$gitclonehttps://github.com/nekitdev/aoc-core.git$cdaoc-core$python-mpipinstall.poetryYou can addaoc-coreas a dependency with the following command:$poetryaddaoc-coreOr by directly specifying it in the configuration like so:[tool.poetry.dependencies]aoc-core="^0.1.2"Alternatively, you can add it directly from the source:[tool.poetry.dependencies.aoc-core]git="https://github.com/nekitdev/aoc-core.git"Extrasaoc-coreprovides an extraext, which installs modules likeiters,funcsandwrapswhich can help solving problems in functional style.ExampleThis example assumesaocis installed and inPATH.We will be solving problem the first ever problem of Advent of Code, that is,2015-01.Firstly, we need to make sure we have the token configured:$aoctokenprinttoken not found (path `/home/nekit/.aoc`)Note that the token is placed in~/.aocby default.Heading over to theAdvent of Codewebsite, we need to trace the requests
and find thesessioncookie. This is the token we need to add:$aoctokenset{TOKEN}And now recheck:$aoctokenprint{TOKEN}Secondly, we need to download the data for the problem:$aocdownloadYear: 2015Day: 01(... lots of brackets ...)And we are met by our input data! In order to run the solutions, we need to save this.$aocdownload--year2015--day01--saveOr, if you want to be quicker:$aocdownload-y2015-d01-sNow we have everything ready to solve the problem!In order to define solutions, we need to figure out three types to use:I, the input type that we parse the data into;T, the answer type for the first part of the problem;U, the answer type for the second part of the problem.Since the problem is about navigating the given string and we return integers,
our types will be:I = str,T = intand possiblyU = int.To define the solution, we need to derive fromSolution:# example.pyfromaoc.solutionsimportSolutionclassYear2015Day01(Solution[str,int,int]):...Note the class name,Year2015Day01. This is the convention for naming solutions,
and all solutions must have their name in theYearYYYYDayDDformat.We also need to define three methods:parse, which takes the data string and returnsI;solve_one, which takesIand returnsT;solve_two, which takesIand returnsU.Part one is rather simple, we need to count the occurrences of(and),
and subtract the latter from the former:fromtypingimportFinalfromaoc.solutionsimportSolutionUP:Final="("DOWN:Final=")"classYear2015Day01(Solution[str,int,int]):defparse(self,data:str)->str:returndatadefsolve_one(self,input:str)->int:returninput.count(UP)-input.count(DOWN)defsolve_two(self,input:str)->int:return0Since we don't yet know what part two is about, let's return0for now.Time to run!$aocrunexample.pyresult for `2015-01`answer one: {ONE}answer two: 0parse time: 1.018ussolve one time: 41.401ussolve two time: 245.0nsWe have our answer for part one, let's submit it!$aocsubmit--year2015--day01--part1{ONE}the answer is correctBy the way, we can submit the answer directly after running the solution,
using the--submit (-s)flag.Now onto part two! We need to find the first position where the floor is-1.Nothing too difficult, here is the solution for part two included:fromtypingimportFinalfromaoc.solutionsimportSolutionUP:Final="("DOWN:Final=")"NEVER_REACHED_BASEMENT:Final="the basement was never reached"classYear2015Day01(Solution[str,int,int]):defparse(self,data:str)->str:returndatadefsolve_one(self,input:str)->int:returninput.count(UP)-input.count(DOWN)defsolve_two(self,input:str)->int:up=UPdown=DOWNfloor=0forposition,characterinenumerate(input,1):# one-based indexingifcharacter==up:floor+=1ifcharacter==down:floor-=1iffloor<0:returnpositionraiseValueError(NEVER_REACHED_BASEMENT)And let's run the solution again:$aocrunexample.pyresult for `2015-01`answer one: {ONE}answer two: {TWO}parse time: 989.0nssolve one time: 41.607ussolve two time: 59.411usWe have our answer for part two, let's submit it!$aocsubmit-y2015-d01-p2{TWO}the answer is correctAnd there we go, we have solved the first ever problem in the Advent of Code!DocumentationYou can find the documentationhere.SupportIf you need support with the library, you can send anemailor refer to the officialDiscord server.ChangelogYou can find the changeloghere.Security PolicyYou can find the Security Policy ofaoc-corehere.ContributingIf you are interested in contributing toaoc-core, make sure to take a look at theContributing Guide, as well as theCode of Conduct.Licenseaoc-coreis licensed under the MIT License terms. SeeLicensefor details.