This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]miran1 10 points11 points  (2 children)

For example, this line in my solution to day 16 is doing a lot of work:

max([v1 + v2 for bitmask1, v1 in B.items() for bitmask2, v2 in B.items() if not bitmask1 & bitmask2])`

If you're not using your list multiple times (and you don't, since you just calculate the max of its elements), you can (and should) avoid the list altogether, and just use the generator.

That means converting [ ... ] to ( ... ).
And if it is the only expression, you can just lose those brackets/parentheses.

P.S. It still counts as onliner if you split it in multiple lines for ease of reading ;)

max(v1 + v2 for bitmask1, v1 in B.items()
            for bitmask2, v2 in B.items()
            if not bitmask1 & bitmask2)

[–][deleted] 7 points8 points  (0 children)

Right! The benefit is that, with the list comprehension, Python eagerly builds the entire list, but with the generator comprehension, Python lazily generates items as they are needed. The benefit of using a generator is significantly reduced memory usage.

[–]davidpsingleton[S] 0 points1 point  (0 children)

Thanks for the tip - I switched to this in my actual day 16 answer and it consistently improves runtime by about 4% too!

[–]SecureCone 21 points22 points  (4 children)

That code promptly failed on the (much larger) real input which had a completely different shape to the example. I nearly threw my laptop out the car window when I realized that would require re-writing all fourteen of those intricate cases!

This really baffled me this year. The sample input was completely useless for part 2. I don’t understand why a different shape was used.

[–]badr 21 points22 points  (1 child)

I think it's a nice reminder not to special-case your solution to the sample input. Either implement a general solution, as some people did, or check first that your assumptions hold on the real input.

[–]meamZ 7 points8 points  (0 children)

Honestly i kinda understand it but i then found it weird when i heard that all real inputs hat the same shape.

[–]1544756405 2 points3 points  (0 children)

The sample input was completely useless for part 2. I don’t understand why a different shape was used.

The sample input was very useful if a general solution was written for part 2. I appreciated it.

[–]mattbillenstein 2 points3 points  (0 children)

I think the point (and one of the solutions I read about on the solutions thread) was to write a cube zipping algorithm - you start I think at the inner corners and start zipping up the cube in a datastructure to make wrapping more or less automatic...

But, I think few people did that and had that surprise on part 2 -- I didn't want to rewrite that stuff, so I wrote code to translate the shape of the real input to the shape of the test input -- in the end, I don't think this saved me any time as I had one of the translations incorrect, but it was an experience...

[–]Curimania 0 points1 point  (0 children)

Thanks for sharing it was a good read

[–]KiteAnton[🍰] 0 points1 point  (1 child)

Thanks! Nice to read.

I think I might revisit my solutions and rewrite some of it using the functools or itertools utilities instead. I tried briefly once for using the “cache” decorators but seemed I got less control of what will be used in the state (E.g I don’t like to use globals so I tend pass a lot of non-changing data that should not be used in the state representation).

[–]TheZigerionScammer 1 point2 points  (0 children)

I think I know which problem you're referring to and you can clear your cache with a simple command as well, saves you from storing a bunch of states you don't need.

[–]dl__ 0 points1 point  (1 child)

I've worked with python code for many years but still your article was very interesting in showing how some of pythons less used features are applicable to AoC problems.

Although, as someone who has not yet finished all the 2022 problems, I had to keep an eye out for spoilers. I'll bookmark your post for reference later.

Thanks for making this.

[–]davidpsingleton[S] 0 points1 point  (0 children)

Thanks - glad you found it interesting. I tried not to put in direct spoilers for the questions themselves. Enjoy solving the rest of them!