2019-12-06 10:45:47 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import re
|
|
|
|
import sys
|
2019-12-08 10:33:19 +01:00
|
|
|
# Regex pattern for reading atandt format
|
2019-12-06 10:45:47 +01:00
|
|
|
atandt_desc_pattern = re.compile(r"^([0-9]+)\t([0-9]+)\t(\S+)$") #^([0-9]+)\t([0-9]+)\t(\S+)$ regex for tab separated columns
|
|
|
|
atandt_accepted_state_pattern = re.compile(r"^[0-9]+$")
|
|
|
|
|
|
|
|
current_state = 0
|
|
|
|
accept_states = set()
|
|
|
|
visited_nodes = []
|
|
|
|
|
|
|
|
state_move = set()
|
|
|
|
|
|
|
|
# function that filters set by starting node and returns all edges that starts in that node
|
|
|
|
def filter_set(xs, filtered_value):
|
|
|
|
return set(filter(lambda x: x[0] == filtered_value, xs))
|
|
|
|
|
|
|
|
|
|
|
|
def dfs(graph, start):
|
|
|
|
visited = set()
|
|
|
|
stack = set()
|
|
|
|
for el in filter_set(state_move, 0):
|
|
|
|
stack.add(el)
|
|
|
|
while stack:
|
|
|
|
edge = stack.pop()
|
2019-12-08 10:33:19 +01:00
|
|
|
#print('Current edge: {edge}'.format(edge = edge))
|
2019-12-06 10:45:47 +01:00
|
|
|
if edge not in visited:
|
|
|
|
visited.add(edge)
|
|
|
|
children = set()
|
|
|
|
for val in filter_set(state_move, edge[2]):
|
|
|
|
stack.add(val)
|
2019-12-08 10:33:19 +01:00
|
|
|
#print('Stack: {stack}'.format(stack = stack))
|
|
|
|
#print('Visited: {visited}'.format(visited = visited))
|
2019-12-06 10:45:47 +01:00
|
|
|
else:
|
|
|
|
return [-1]
|
|
|
|
return visited
|
|
|
|
|
|
|
|
|
|
|
|
for line in sys.stdin:
|
|
|
|
line = line.replace('\n', '')
|
|
|
|
move = atandt_desc_pattern.match(line)
|
|
|
|
succes = atandt_accepted_state_pattern.match(line)
|
|
|
|
if move:
|
|
|
|
node = str(move.group(3)).decode('utf-8')
|
|
|
|
state_move.add((int(move.group(1)), node, int(move.group(2))))
|
|
|
|
if succes:
|
|
|
|
accept_states.add(int(line))
|
|
|
|
print('Moves: {state_move}'.format(state_move = state_move))
|
|
|
|
print('Accept states: {accept_states}'.format(accept_states = accept_states))
|
|
|
|
|
|
|
|
vis = dfs(state_move, 0)
|
|
|
|
if -1 in vis:
|
2019-12-08 10:33:19 +01:00
|
|
|
print("TAK")
|
2019-12-06 10:45:47 +01:00
|
|
|
else:
|
2019-12-08 10:33:19 +01:00
|
|
|
print("NIE")
|