Advent of Code 2020
Solutions for 2020
We’re going on a holiday!
- Find two entries that sum to 2020
- Three entries that sum to 2020
- This could be more efficient in R but I don’t know how to break out of a double loop.
- While searching for loop options in python I found three different ways to write the answer.
- How many valid passwords? Number of letters is given
- How many valid, but the position of the letters is given
- At first
str_split()andunnest_wider()seemed like the way to go butseparate()is much better. pmap()really takes care of business after defining a check function- Lists can take care of many things in python, especially with the asterisk operator
- Multiple returns from functions are great
- Trees grow weirdly; how many do you hit when riding on your toboggan? Right 3, down 1
- There are different ways of getting down - multiply them all
- We don’t need to replicate and create a huge matrix or dataframe, using modulo is much simpler
- This might be my favourite code from the advent
- Slightly different logic for python and R with the modulo
- Passport checking - which records have the required fields?
- Passport checks are now stricter: do the fields have the right content?
- Welcome to the regex portion of proceedings
- The formatting of the input was awkward but ultimately straightforward to parse
- Maybe we want one function (python), maybe we want many functions (R)
- I think I prefer
parse_integer()toint(re.search('\\d+', itm).group(0))
- The boarding passes are in binary?! What is the highest seat number?
- Which seat number is missing?
- Pretty pleased with myself to recognise this as binary, which makes the code kinda simple
strtoi()has abaseargument which makes converting to binary very simple- Having a look again,
str_replace_all()works within mutate - python list comprehensions are so great
int()in python also has a base argument!
- Sum the count of the letters for the passengers on the plane
- Sum the count of the common letters for each group
- Looking back at this I’m not sure why I made part two so difficult
- List comprehensions work great for this one too in python
count()with an argument is nice
- How many bags can hold a shiny gold bag?
- How many bags can fit inside a shiny gold bag?
- This was the last to be completed
- The recursion and branching was difficult to work with
- Run a sequence of operations for a handheld game console until we find an infinite loop
- Find the corrupt record to prevent an infinite loop
- This was the point at which I decided to only do R solutions rather than R and py
- Create a function and track where we have already visited; stop when we find a loop
- Part two is likely inefficient, but it works, go through and change nop and jmp
- Find the first invalid number, where a valid number is the sum of any pair of the previous 25 numbers
- Find a contiguous list of numbers that add to the invalid number in part one
- For loops play a big part here, I’m not sure if there’s any way around that
- Both parts run really fast so don’t worry
- An interesting contrivance to chain together adapters to charge a phone - what are the differences in jolts?
- How many different combinations could that chain of adapters use?
- This was on the bus home after work in a google sheet
- It feels like there was a trick to be discovered, and somehow it was uncovered by me
- I am not totally sure about the code converted from the sheet but it works
- People don’t like sitting next to each other, how many seats end up occupied?
- People don’t like seeing other people, how many seats end up occupied?
- Write some functions to add people to seats and remove people from seats
- TODO: The functions don’t seem to work as written
- Follow the instructions for the movement of the ship, where do you end up?
- The instructions are different and difficult, where do you end up?
- Work through the instructions and make sure we write things down correctly.
- Write a function to deal with the rotation, and loop through like before