Hive/ant.py
2024-01-12 22:06:42 +01:00

77 lines
No EOL
2.4 KiB
Python

import sys
neighbors = lambda p, q: [(p + dp, q + dq) for dp, dq in [(0, -1), (1, -1), (1, 0), (0, 1), (-1, 1), (-1, 0)]]
def crawler(table, visitation_rights, pos):
visitation_rights.add(pos)
resulted_offence = list() # just a result but cause I renamed visited to visitation_rights, I also renamed this.
for i in table[pos][0]:
touching_hive, empty_space = False, False
if not i in table:
continue
if i not in visitation_rights and not table[i][1]:
for j in table[i][0]:
if empty_space and touching_hive:
break
if j in table[pos][0]:
if not j in table:
continue
if not table[j][1] or j in visitation_rights:
empty_space = True
else:
touching_hive = True
if empty_space and touching_hive:
resulted_offence += [i] + crawler(table, visitation_rights, i)
return resulted_offence
def flood(table, visited, pos):
n = 0
for i in table[pos][0]:
if i not in table:
continue
if i not in visited and table[i][1] != 0:
visited.add(i)
n += 1 + flood(table, visited, i)
return n
def seperation_check(table, figs, pos):
for i in table[pos][0]:
if i not in table:
continue
if not table[i][1]:
continue
visitation_rights = set() #orginally visited, but for some reason I decided to name it like this.
visitation_rights.add(pos)
visitation_rights.add(i)
return figs-2 == flood(table, visitation_rights, i)
return False
def main():
with open(sys.argv[1],"r") as f:
lines = f.readlines()
lines = [list(map(int, l.split(" "))) for l in lines]
starting_ant = None
lookup_table = dict()
total_stones_on_board = 0
for l in lines:
lookup_table[(l[0],l[1])] = [neighbors(l[0],l[1]),l[2]]
if l[2] != 0:
total_stones_on_board +=1
if l[2] == 2:
starting_ant = (l[0],l[1])
if not seperation_check(lookup_table, total_stones_on_board, starting_ant):
print([])
return
res = crawler(lookup_table, set(), starting_ant)
res.sort()
print(list(map(list,res)))
if __name__=="__main__":
main()