+/foobar
google programming puzzles
git clone https://git.julia.blue/+/foobar
6422597
master
1from operator import mul 2from fractions import gcd 3 4vec_mul = lambda a ,b :tuple (map (lambda x :mul (* x ),zip (a ,b ))) 5 6vec_scale = lambda a ,b :vec_mul (a , (b ,b )) 7 8vec_add = lambda a ,b :tuple (map (sum ,zip (a ,b ))) 9 10vec_sub = lambda a ,b :vec_add (a ,vec_scale (b ,- 1 )) 11 12vec_magnitude = lambda a :sum (map (lambda x :x ** 2 ,a ))** .5 13 14 15def get_mirror (pos ,coords ,dimensions ): 16temp = [ 17pos [i ]if coords [i ]% 2 == 0 else dimensions [i ]- pos [i ] 18for i in range (2 ) 19 ] 20return vec_add (temp ,vec_mul (coords ,dimensions )) 21 22 23def vec_reduce (a ): 24if 0 in a : 25return tuple (map (lambda x :x / abs (x )if x > 0 else 0 ,a )) 26return tuple (map (lambda x :x / gcd (* map (abs ,a )),a )) 27 28 29def answer (dimensions ,us ,target ,distance ): 30selfs = {} 31hits = set ([]) 32bound = [distance / dimensions [i ]+ 1 for i in range (2 )] 33possible = [range (- i ,i + 1 )for i in bound ] 34 35for x in possible [0 ]: 36for y in possible [1 ]: 37current = vec_sub (get_mirror (us , (x ,y ),dimensions ),us ) 38d = vec_magnitude (current ) 39current = vec_reduce (current ) 40if d <= distance : 41if current in selfs and selfs [current ]< d : 42continue 43selfs [current ]= d 44 45for x in possible [0 ]: 46for y in possible [1 ]: 47current = vec_sub (get_mirror (target , (x ,y ),dimensions ),us ) 48d = vec_magnitude (current ) 49current = vec_reduce (current ) 50if d <= distance : 51if current in selfs and selfs [current ]< d : 52continue 53hits .add (current ) 54 55return len (hits ) 56 57 58dimensions = [3 ,3 ] 59your_position = [1 ,1 ] 60guard_position = [2 ,1 ] 61distance = 195 62 63# dimensions = [300, 275] 64# your_position = [150, 150] 65# guard_position = [185, 100] 66# distance = 5000 67 68# dimensions = [6, 15] 69# your_position = [3, 13] 70# guard_position = [4, 11] 71# distance = 7455 72 73answer (dimensions ,your_position ,guard_position ,distance )