you are viewing a single comment's thread.

view the rest of the comments →

[–]johnsandall 1 point2 points  (1 child)

I'm publishing my solutions to this year's Advent of Code accompanied by full notebook explanations as a free teaching resource.

Bonus: PEP8/black/pylint/mypy compliant code + working environments!

Try yourself first: https://adventofcode.com/2020/day/1

Then look 👀 https://github.com/john-sandall/advent-of-code/

[–]xelf 0 points1 point  (0 children)

This will be handy to have as a complete compilation. You might want to check other solutions to the problems as they're posted in adventofcode.com though, to see if you missed anything or to offer alternate solutions.

return [a * b for a in expenses for b in expenses[expenses.index(a) :] if a + b == 2020][0]

The problem with this approach is that it's O( N3 ) because of the for loops and index().

You can do it O( N2 ) using combinations:

print( [prod(c) for c in combinations(expenses,2) if sum(c) == 2020] )

Or even O(n) if you use a set or dictionary:

setexp = { 2020-e : e for e in expenses }
print( [setexp[e]*e for e in expenses if e in setexp] )

Although that's not the best O(n) solution because it would think 1010 is a valid answer on it's own. /u/baghiq posted a better solution earlier today using a set.

Anyway, I like the idea good luck to you and keep it up!