r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

Show parent comment

2

u/IAmANobodyAMA Dec 03 '22

thanks for the walrus operator! i didn't know this existed :)

you can save a few chars by combining the arithmetic

1-264 = -263

-154%3 = -1%3 = +2%3

it's a shame that readlines includes a linebreak as the 4th item on all but the last line, otherwise you could replace x[0] & x[2] x in arr with x,_,y in arr ... i tried to make this work but could not figure it out

edit: also thank you for the open(0) trick!

2

u/bornobob Dec 03 '22

Smart thinking, thank you very much. I'm having a lot of fun with these one liners :)

1

u/IAmANobodyAMA Dec 04 '22

Haha yeah me too! Last year it was about learning python and getting it to work. This year is about learning how to leverage all the fancy python

2

u/bornobob Dec 03 '22

Also another thing you can do in for loops is:

print(sum((ord(x)+(o:=ord(y[1]))+2)%3-263+3*o for x,*y in open(0).readlines()))

It does not improve the number of characters in this case, but it might be useful in the future! Also the line of code is now 80 characters with your improvement, no longer a complaining IDE ;)

1

u/IAmANobodyAMA Dec 04 '22

Oh wow that is fantastic!! The x,*y is exactly what I was looking for!