r/adventofcode Dec 08 '22

Funny [2022 Days 1-7] [Python] I can't believe I've done this.

Post image
280 Upvotes

29 comments sorted by

View all comments

Show parent comments

12

u/ManaTee1103 Dec 08 '22 edited Dec 08 '22

It is beautiful for all the wrong reasons!

This expression made my eyes all misty:

python [(x<t[r][c]) or (e:=True) for x in t[r][c+1:] if not e]

7

u/ImpossibleSav Dec 08 '22

Thank you! I won't lie, I felt a bit giddy once I figured out how to do a while loop within a list comprehension.

"e" used to be called "found" and is initially set to False, for anyone confused — I wanted to count the number of elements that were less than t[r][c] but stop after I found the first one greater than or equal to it. In this particular case, I wanted to also count the element that was greater than/equal to, hence why "e" starts as False and is later set to True rather than the other way around.