about summary refs log tree commit diff
path: root/others/other/khaosat.py
blob: 861d6f514179ece0d7b1269696be80482943e47f (plain) (blame)
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python3
DIRECTIONS = {'E': (1, 0), 'W': (-1, 0), 'S': (0, -1), 'N': (0, 1)}

current, memory = (0, 0), {(0, 0): 0}
with open('KHAOSAT.INP') as fi, open('KHAOSAT.OUT', 'w') as fo:
    for c in fi.read().rstrip():
        prev, current = current, tuple(map(sum, zip(current, DIRECTIONS[c])))
        memory.setdefault(current, memory[prev] + 1)
    print(memory[current], file=fo)