Skip to content

Commit 7a7bd25

Browse files
committed
Round 1B
1 parent 5ac82cf commit 7a7bd25

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,13 @@ My solutions to [Google Code Jam 2020](https://codingcompetitions.withgoogle.com
2626
| A | [Pattern Matching](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd74/00000000002b3034) | [Solution](https://github.com/theXYZT/codejam-2020/blob/master/Round%201A/pattern-matching.py) | |
2727
| B | [Pascal Walk](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd74/00000000002b1353) | [Solution](https://github.com/theXYZT/codejam-2020/blob/master/Round%201A/pascal-walk.py) | |
2828
| C | [Square Dance](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd74/00000000002b1355) | [Solution](https://github.com/theXYZT/codejam-2020/blob/master/Round%201A/square-dance.py) | |
29+
30+
31+
## [Round 1B](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fef2)
32+
33+
| # | Problem | Solution | Notes |
34+
|---|---------|----------|-------|
35+
| A | [Expogo](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fef2/00000000002d5b62) | [Solution](https://github.com/theXYZT/codejam-2020/blob/master/Round%201B/expogo.py) | |
36+
| B | [Blindfolded Bullseye](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fef2/00000000002d5b63) | [Solution](https://github.com/theXYZT/codejam-2020/blob/master/Round%201B/blindfolded-bullseye.py) | |
37+
| C | [Join the Ranks](https://codingcompetitions.withgoogle.com/codejam/round/000000000019fef2/00000000002d5b64) | [Solution]() | |
38+

Round 1B/blindfolded-bullseye.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Codejam 2020, Round 1B: Blindfolded Bullseye
2+
3+
import sys
4+
from random import randint
5+
6+
7+
class Case:
8+
def __init__(self):
9+
self.queries_left = 300
10+
11+
def query(self, X, Y):
12+
self.queries_left -= 1
13+
print("{} {}".format(X, Y), flush=True)
14+
response = input()
15+
if response == 'WRONG':
16+
sys.exit()
17+
return response
18+
19+
def binary_search_left(self, right, func):
20+
left = -10**9
21+
while left < right:
22+
m = (left + right) // 2
23+
f = func(m)
24+
if f == 'HIT':
25+
right = m
26+
elif f == 'MISS':
27+
left = m + 1
28+
else:
29+
return None
30+
return left
31+
32+
def binary_search_right(self, left, func):
33+
right = 10**9
34+
while left < right:
35+
m = (left + right) // 2
36+
f = func(m)
37+
if f == 'HIT':
38+
left = m + 1
39+
elif f == 'MISS':
40+
right = m
41+
else:
42+
return None
43+
return right - 1
44+
45+
def solve(self):
46+
x, y = 0, 0
47+
r = self.query(x, y)
48+
while r == 'MISS':
49+
x, y = randint(-10**9, 10**9), randint(-10**9, 10**9)
50+
r = self.query(x, y)
51+
52+
if r == 'CENTER':
53+
return
54+
55+
x_low = self.binary_search_left(x, lambda x: self.query(x, y))
56+
if x_low is None:
57+
return
58+
59+
x_high = self.binary_search_right(x, lambda x: self.query(x, y))
60+
if x_high is None:
61+
return
62+
63+
y_low = self.binary_search_left(y, lambda y: self.query(x, y))
64+
if y_low is None:
65+
return
66+
67+
y_high = self.binary_search_right(y, lambda y: self.query(x, y))
68+
if y_high is None:
69+
return
70+
71+
r = self.query((x_low + x_high) // 2, (y_low + y_high) // 2)
72+
if r != 'CENTER':
73+
sys.exit()
74+
75+
76+
# I/O Code
77+
num_cases, A, B = map(int, input().split())
78+
79+
for _ in range(1, num_cases + 1):
80+
case = Case()
81+
case.solve()
82+
83+
sys.exit()

Round 1B/expogo.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Codejam 2020, Round 1B: Expogo
2+
3+
from functools import lru_cache
4+
5+
6+
@lru_cache(maxsize=None)
7+
def solve(X, Y):
8+
if X == Y == 0:
9+
return True, ''
10+
11+
if X % 2 == Y % 2:
12+
return False, None
13+
14+
if X % 2 == 1:
15+
# Jump West
16+
if (X, Y) != (1, 0):
17+
possible, jumps = solve((X + 1) // 2, Y // 2)
18+
if possible:
19+
return True, 'W' + jumps
20+
21+
# Jump East
22+
if (X, Y) != (-1, 0):
23+
possible, jumps = solve((X - 1) // 2, Y // 2)
24+
if possible:
25+
return True, 'E' + jumps
26+
27+
else:
28+
# Jump South
29+
if (X, Y) != (0, 1):
30+
possible, jumps = solve(X // 2, (Y + 1) // 2)
31+
if possible:
32+
return True, 'S' + jumps
33+
34+
# Jump North
35+
if (X, Y) != (0, -1):
36+
possible, jumps = solve(X // 2, (Y - 1) // 2)
37+
if possible:
38+
return True, 'N' + jumps
39+
40+
return False, None
41+
42+
43+
# I/O Code
44+
num_cases = int(input())
45+
for case in range(1, num_cases + 1):
46+
X, Y = map(int, input().split())
47+
48+
possible, jumps = solve(X, Y)
49+
if possible:
50+
print('Case #{}: {}'.format(case, jumps))
51+
else:
52+
print('Case #{}: IMPOSSIBLE'.format(case))

0 commit comments

Comments
 (0)