knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
These are my solutions to Advent of Code 2020, a series of 25 programming puzzles.
You can install adventofcode20 from GitHub with:
# install.packages("devtools") devtools::install_github("tjmahr/adventofcode20")
There is probably only one reason to install this package:
use_day()
is a usethis-style function to create
placeholder files for each day.
use_day(1) #> Executing: pandoc -t markdown -o #> "C:\Users\Tristan\AppData\Local\Temp\RtmpmajieH\file21d052ca5862.markdown" #> "C:\Users\Tristan\AppData\Local\Temp\RtmpmajieH\file21d052ca5862.html" #> √ Writing 'R/day01.R' #> * Modify 'R/day01.R' #> ● Write your solution code here #> ● Once you unlock Part Two, update the Roxygen block with the description #> √ Writing 'inst/input01.txt' #> * Modify 'inst/input01.txt' #> ● Copy your problem input into this file #> √ Writing 'tests/testthat/test-day01.R' #> * Modify 'tests/testthat/test-day01.R' #> ● Write unit tests using the examples from the problem description #> √ Writing 'inst/run-day01.R' #> * Modify 'inst/run-day01.R' #> ● Run your solution on the input here. Once it works, update #> R/data-solutions.R
This recipe, called on day 1:
The /R
folder contains the R functions I wrote for each day. I used some light
test-driven development for the puzzles. That is, each puzzle description
provides some example inputs and outputs. Before tackling the main test input, I
write a unit-test in /tests
that confirms that my solution can correctly
reproduce the examples. The /inst
directory contains the code to handle the
main test input for each day.
I limited the amount of package dependencies used for these puzzles to maximize future compatibility and to make sure that it is mostly my code that solves the problems. For example, if a puzzle requires answering questions about the data in a tree-like structure, it would be kind of cheating for me to find a library for building and traversing trees to tackle the problem. It's advent of code, not advent of load.
I have allowed myself to use:
%>%
syntaxI've put my R source code under a GPL-3 license. It should not apply to the puzzle descriptions in the code comments at the top of each file. I did not write those descriptions.
Here are the programming tasks and techniques I used for the various puzzles.
By "book-keeping", I mean basic programming where I keep track of some changing state like a position in a vector.
By "math", I mean studying the problem and using math to find a shortcut that lets me skip some computations.
Here are some functions that have I discovered, rediscovered, or otherwise appreciate somewhat more from these exercises:
complete.cases()
intersect()
lengths()
modifyList()
outer()
prod()
strtoi()
chartr()
Find() and Position()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.