+/foobar

google programming puzzles

git clone https://git.julia.blue/+/foobar

Joseph RyanStart on sets6422597

master
1.9 KiB73 linesraw
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):
16    temp = [
17        pos[i] if coords[i] % 2 == 0 else dimensions[i] - pos[i]
18        for i in range(2)
19    ]
20    return vec_add(temp, vec_mul(coords, dimensions))
21
22
23def vec_reduce(a):
24    if 0 in a:
25        return tuple(map(lambda x: x / abs(x) if x > 0 else 0, a))
26    return tuple(map(lambda x: x / gcd(*map(abs, a)), a))
27
28
29def answer(dimensions, us, target, distance):
30    selfs = {}
31    hits = set([])
32    bound = [distance / dimensions[i] + 1 for i in range(2)]
33    possible = [range(-i, i + 1) for i in bound]
34
35    for x in possible[0]:
36        for y in possible[1]:
37            current = vec_sub(get_mirror(us, (x, y), dimensions), us)
38            d = vec_magnitude(current)
39            current = vec_reduce(current)
40            if d <= distance:
41                if current in selfs and selfs[current] < d:
42                    continue
43                selfs[current] = d
44
45    for x in possible[0]:
46        for y in possible[1]:
47            current = vec_sub(get_mirror(target, (x, y), dimensions), us)
48            d = vec_magnitude(current)
49            current = vec_reduce(current)
50            if d <= distance:
51                if current in selfs and selfs[current] < d:
52                    continue
53                hits.add(current)
54
55    return 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
73print answer(dimensions, your_position, guard_position, distance)