Advent of Code

Advent of Code 2019 starts tomorrow. The leaderboard code is still 111686-e01a629f.

2 Likes

Since tomorrow is Sunday, I promise to at least do day 1, lol.

1 Like

I thought I had more time. But hey, it’ll be nice to do some Python again for a bit.

I’ve been meaning to learn kotlin so I guess I’ll use that this year.

No shit talk. I’m gonna do it. Just waiting for my dev machine to upgrade.

Completed both stars on day 2

Got both stars on day 3. The difference between the first star and second star only required changing 3 lines.

I know it’s still super easy on day 3, but I feel it necessary to point out that every answer I have submitted is correct on the first try. The Advent web site has never told me “you are wrong, try again.”

My first answer for day 2 part 2 was wrong because I misread the instructions, but same otherwise.

Indeed. At this point I’m most nervous about misreading the instructions. Yet, I’m not nervous enough to actually put the effort in to read them extra thoroughly.

Solved Day 4.

Part 1 was a breeze like usual.

Part 2 actually took me some minutes to solve. Mostly because I decided to use a regular expression, and it took some time to read the documentation you have to read every time you use regular expressions. I still got the correct answer on the first execution of the code. Can’t deny, I was actually a little nervous when I typed in the number that the site would tell me I was wrong.

Also, I just realized how the score is calculated. It’s based on who solves it first! I’m not even working on these things until I get home from work at night, just a few hours before the next puzzle is released.

Booooo

That’s also when I’m doing them. I’m not going to compete on time.

For 4-2 I just did a character count instead of regex, it pretty much amounts to the same thing since you’re guaranteed a sorted string for valid answers.

Also, I looked at Scott’s code briefly and… my code is pretty sloppy in comparison. I’ve always primarily been a C++/Java programmer so I’ve never gotten fully used to elegant Python code.

Code that works, works. There are no style points in Advent of Code. Just get the right answer.

As for my personal coding style, it only looks elegant because of three tricks I use. I will teach you all three tricks.

First, I use variables names to try to create plain English code. I learned this from the book “Code Complete”

example:

I used to write code like this:

if x < 3 and x > -3:

Nowadays I try to write code like this

x_is_less_than_three = x < 3
x _is_greater_than_negative three = x > -3

if x_is_less_than_three and x_is_greater_than_negative_three:

Trick 2: Comprehensions are amazing.

Old Scott would write:

numbers = [1,2,3,4,5]
even_numbers = []
for n in numbers:
    if n % 2 == 0:
        even_numbers.append(n)

Scott that uses comprehensions whenever possible writes:

numbers = [1,2,3,4,5]
even_numbers = [x for x in numbers if x % 2 == 0]

The third trick, and my favorite trick, is to create data-driven control structures.

In the old days I would have used an if/then or case-based structure;

if opcode == 1:
   do this
elif opcode == 2:
    do this

Because functions are objects in Python, and because dictionaries exist, I can create the kind of structure seen in my solution for day 2.

Welp. Day 5 looks time consuming. Not getting around to that anytime soon.

Pretty happy with my monotonically increasing digits test.

    sn = str(n)
    ln = list(sn)
    if sorted(ln) != ln:
        continue

OMG that is so obvious in hindsight.

Not really an improvement, but it’s cool to point out that you can actually call sorted() on sn directly in that example. Doesn’t help though, because the output of sorted() can’t be compared to sn directly. You either need to use ''.join() on the output of sorted or to make ln, as you have done.

It’s looking like a lot of vacation coming for me this December, unless my company changes their mind. That means I’ll probably make more progress on this than I ever have before. I can’t promise I’ll finish, though, obviously.

I’ve kind of had the urge to go back and do some from previous years as well. Might do a little of that this month. We’ll see.

https://www.youtube.com/watch?v=MAlSjtxy5ak