A Very Python Christmas

     Advent of Code has been going on, which is a lot of fun! I'm trying to keep up while also tending to my school and work responsibilities, but Advent of Code has been a fun way to remind myself that coding is fun. Each day includes a puzzle where you pretend to be Santa, solving problems by writing code. I chose to write my solutions in Python, simply because I knew it would be easy to set up and start coding right away.

    Anyway the first day is a relatively simple puzzle, with the puzzles getting more complex as the days go by. You can see all my solutions as I work on them here, but I thought I would share my process for solving the puzzle on Day 2. On day 2 we are tasked with fixing the North Pole password database that has become corrupted. We are given a list that shows a list of password policies and their passwords. We need to determine how many of these are valid.

        1-3 a: abcde
        1-3 b: cdefg
        2-9 c: ccccccccc

    So the first thing I do with this list is to break my list into individual lines (that's what my main function does), then pass that list of lines to my Part 1 function. Then, each of those lines needs to be broken into the important parts; the minimum and maximum amount that each letter should appear, the letter that will appear, and the password. Once those attributes are located and put into variables, all I had to do was count the amount of occurrences of my specific character in each password, and determine if that falls in between the min and max amount of occurrences. 

    But once you've got Part 1 completed, Part 2 changes things up a bit. It turns out my min and max are really the location the character should occur and a location where the character should not occur. This isn't so bad of a change, since we are already breaking the code apart in our last function, we just need that code interpret the min and max differently and then check the password differently. So instead, we call our min and max first and second, and now we just check the locations in the password. But wait, I misread the instructions. From the two locations we are given, one of them should contain the letter but not both, so the second needs to contain our letter if the first one doesn't. So we throw that onto our if statement, easy as pie.

 

Comments

Popular posts from this blog

API's in C#

Using WebRTC to build a videophone in React and TypeScript

Reviewing WPF and MVVM