Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions 2014/q2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
def get_scores(size, tiles):
def v(i, j):
nonlocal size
return not (
((i + 1) % size == 0 and j == i + 1)
or (i % size == 0 and j == i - 1)
or (j < 0)
or (j >= len(tiles))
)

t = lambda i, adj: list(filter(lambda j: v(i, j), adj))

red_adj = {i: [] for i in range(len(tiles))}
gre_adj = {i: [] for i in range(len(tiles))}
for (i, tiletype) in enumerate(tiles):
if tiletype == "1":
red_adj[i] = t(i, [i - size, i + size])
gre_adj[i] = t(i, [i - 1, i + 1])
if tiletype == "2":
red_adj[i] = t(i, [i - 1, i + 1])
gre_adj[i] = t(i, [i - size, i + size])
if tiletype == "3":
red_adj[i] = t(i, [i - size, i - 1])
gre_adj[i] = t(i, [i + 1, i + size])
if tiletype == "4":
red_adj[i] = t(i, [i - size, i + 1])
gre_adj[i] = t(i, [i - 1, i + size])
if tiletype == "5":
red_adj[i] = t(i, [i + 1, i + size])
gre_adj[i] = t(i, [i - size, i - 1])
if tiletype == "6":
red_adj[i] = t(i, [i - 1, i + size])
gre_adj[i] = t(i, [i - size, i + 1])

for adj in [red_adj, gre_adj]:
for root in adj:
for (i, node) in enumerate(adj[root]):
if root not in adj[node]:
adj[root].pop(i)

def cycles(adj):
nonlocal tiles
state = ["unvisited"] * len(tiles)
parents = {}
times = {}
time = 0
score = 0

def DFS(root):
nonlocal time, score
if state[root] == "unvisited":
state[root] = "visited"
times[root] = time
time += 1
for node in adj[root]:
if state[node] == "visited":
if node != parents[root]:
score += times[root] - times[node] + 1
continue
parents[node] = root
DFS(node)
state[root] = "processed"

for i in range(len(tiles)):
DFS(i)

return score

return (cycles(red_adj), cycles(gre_adj))


if __name__ == "__main__":
size = int(input("> "))
tiles = []
for _ in range(size):
row = input("> ").split()
tiles += row

scores = get_scores(size, tiles)
print(scores[0], scores[1])
35 changes: 35 additions & 0 deletions 2014/q3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from math import comb

n = int(input("> "))


def password(n):
char = lambda i: chr(65 + i) if i <= 25 else str(i - 26)

lowest_d_digit = 0

d = 0
for i in range(12):
lowest_d_digit = sum([
comb(36, 11 - i - j)
for j in range(11 - i)
if j + i < 11
])
if n > lowest_d_digit:
d = 12 - i
break

if n <= 36:
print(char(n - 1))
else:
nth_d_digit = n - lowest_d_digit - 1
acc = 0
for i in range(36):
acc += comb(35 - i, d - 1)
if nth_d_digit < acc:
print(char(i), end="")
password(n - acc)
break


password(n)