DEV Community

Ben Sinclair
Ben Sinclair

Posted on

What I've learned from Advent of Code 2019. Already.

Now Advent of Code 2019 is live and the first challenge is out. I didn't really do it last year, though I did flip through a couple of the questions. And I solved one of them in Vim, because that's the kind of fool I am.

I wanted to post, in a slightly excited manner, about my experience so far this year. I've made a private leaderboard for friends at my office, and we're going to see how we all get along, and chat over Slack and coffee, that sort of thing. So it's a team-community-building phenomenon, I guess.

But what I wanted to talk about really, is how this has woken me up this morning. Where I am, it's 7:30 (or it was when I started typing) and I've had half a coffee.

I decided to do the first challenge in Python.

I haven't used Python for years. It used to be my favourite language, but then I switched jobs and ended up doing PHP and Javascript all day and kind of forgot how I used to, you know, really enjoy programming. I've meant to pick it back up for a long time. I've meant to go along to one of the Python meet-ups again for_ever_ but something always comes up.

I had to Google things.

Well, DuckDuckGo them, at any rate.

I learned stuff.

My first thought with this challenge (it's a take on the rocket equation but pared down to a few operations list of integers) was to use reduce until I found out... reduce isn't part of core Python anymore. Yeah, that's how out of date I am. It's still there, in functools but you have to explicitly import it. Why is that? map is still there.

It's because of the old Benevolent Dictator:

So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly.

And you know what? I love that someone actually said that. I see people using big-arrow functions in Javascript that look like they could fit on five lines and then they're stuffed into a reduce and part of me's trained itself to think that's just the way things are done now.

It reminds me that the Zen of Python exists.

Simple is better than complex.

This is a good Christmas present.

Top comments (1)

Collapse
 
anze3db profile image
Anže Pečar

I agree 100% about reduce and I would extend the same logic to map as well. In most places where you would usually use map a list comprehension is probably more readable!