Advent of Code Days 1-5

Advent of Code 2020 Map

I hope everyone is having a great holiday season. Once again, this year I decided to challenge myself to work on my python and complete The Advent of Code. My goal was to stick to each and every day, and for the most part, I was able to stick with that. I worked on each day’s problem on the day it came out (with some exceptions) and generally got at least the first half of it done that day. There are a few days where I wasn’t able to complete the second half and one or two where I didn’t get the first half either but I plan on continuing to work on those.

Day 1

Essentially what Day 1 is looking for, is the only two numbers (out of a list of 200) that when you add them together is equal to 2020. Part two is the same thing, only it wants three numbers instead of two. This was a really simple one where I just imported all of the numbers into a list and used the combinations function from intertools to put together every combination possible (first of two numbers and later three numbers) until I found the set that added up to 2020. All and all, a pretty simple problem.

My solution can be found here.

Day 2

Day 2 part one is giving you a list of possible passwords, along with a rule that says if the password is valid. Each line looks similar to 4-8 n: dnjjrtclnzdnghnbnn where the two numbers represent a range (in this cast 4-8 inclusive), the letter is a character that must appear within the password that many times, and then the other password. If the password fits the rule, count it.

To solve, I split the line up into space separated elements and did a count of how many times the character (element 1) appears in element 2. Then I split element 0 (the range) using the ‘-‘ as a delimiter and compared the count with the numbers found in [0][0] and [0][1].

The second part tells us that we messed up the rule and what the rule is actually telling us is that that specified character must be found in either (but not both) position x or y. The start of this was exactly the same (just space seperate the line) but this time I used the XOR function to see if the character at that location in the string matched.

My solution can be found here.

Day 3

Day 3 wants us to figure out how many times we run into a tree following a specified path while reading our toboggan. The first trick to this is that we have to follow a specified pattern (3 right forever 1 down) until we get to the bottom of the map. Unfortunately, the map is much longer than it is wide, but it just repeated indefinitely for as long as we need. Here I ended up deciding to figure out how wide the map is, and use that as my mod so that no matter how far right I moved, it would just put me back in the appropriate position on the map. Keep doing that until you hit the bottom and see how many times you ran into a tree.

Part two was the same, only they specified multiple slopes to check instead of just the one that we had in the first part of the problem.

My solution can be found here.

Day 4

Day 4 wants us to look for valid ‘North Pole Credentials’. In order to be valid, it has to have every required field. Unfortunately the input that we aren’t provided doesn’t have every field on a single line. Some lines contain one field, while other lines have multiple fields with a blank line separating each set of credentials.

To do this I go through line by line. If the line has anything in it, I separate it by ‘,’ and then again by ‘:’ to give me the keys to each field. I then add the key and its value to a dict for that particular set of credentials. Once I get to the end of that credential (a blank line), I appended it to a list of credentials (I figured that the credentials themselves would be important in part to so better be prepared). Once done, I went through credential by credential in the list, and check to make sure each of the required fields was present and just counted.

For part two, wouldn’t you know, the contents of the credentials was important. Now not only did we have to check and make sure all of the fields are present, but now we have ot make sure that the value it contains is valid as well.

To do this, I figured the best thing to do was create individual functions to check each field. The value of the field was passed to the corresponding function which ran it against an appropriate set of rules for that function. It was good, then great and if not, it fails and moves on to the next set of credentials

My solution can be found here.

Day 5

This was an odd problem that I way over-complicated at first. Basically you are getting on the plane and need to find your seat but can’t find your ticket but some how you can see everyone else’s ticket. But the ticket doesn’t use row/seat numbers. Instead it binary encodes the seat location using a combination of ‘F’ or ‘B’ to represent the front or back half of a section or ‘L’ or ‘R’ to represent the left or right part of a row.

I tried a few different ways to tackle this that were just not making a lot of sense and then I realized that I had not given a keyword in the description enough attention (BINARY). After that, I decided just to turn letters into numbers. F and L both became ‘0’ and B and R became ‘1’ using a simple string.replace(function). Then after reading up on how to do it, I discovered you can turn a binary formatted string into an integer using the int(string, 2) function (the 2 represents base 2 numbering).

The second part of this was finding your exact seat. Yours was the only seat not filled on the flight so you have to find the empty seat. I just arranged the integer number that each seat description corresponds to, and looped through them until I found a missing number.

My solution can be found here.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>