+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
a7718a0
master
1from functools import cmp_to_key 2 3 4def cmp (a ,b ): 5typeof = lambda x :repr (type (x ))[8 ] 6match (typeof (a ),typeof (b )): 7case ("i" ,"i" ): 8n = a - b 9return n and n // abs (n )or 0 10case ("l" ,"l" ): 11for i in range (min (len (a ),len (b ))): 12if tmp := cmp (a [i ],b [i ]): 13return tmp 14return cmp (len (a ),len (b )) 15case ("i" ,"l" ): 16return cmp ([a ],b ) 17case ("l" ,"i" ): 18return cmp (a , [b ]) 19 20 21lines = list (map (eval ,filter (lambda x :x ,open ("input" ).read ().splitlines ()))) 22pairs = list (zip (lines [::2 ],lines [1 ::2 ])) 23sum (- (cmp (* p )- 1 )// 2 * (i + 1 )for i ,p in enumerate (pairs ))) 24all = [[]]+ lines + [[[2 ]], [[6 ]]] 25all .sort (key = cmp_to_key (cmp )) 26all .index ([[2 ]])* all .index ([[6 ]]))