92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
file = "./input.txt"
|
|
# file = "./ex.txt"
|
|
from copy import deepcopy
|
|
import sys
|
|
sys.setrecursionlimit(6500)
|
|
from time import time
|
|
|
|
start = time()
|
|
def guard_position(game_field) -> (int, int):
|
|
for li in range(game_field_lines):
|
|
for ro in range(game_field_rows):
|
|
if game_field[li][ro] == '^':
|
|
print(f'GuardPosition: line:{li}, row:{ro}')
|
|
return li, ro
|
|
|
|
def go_up(gl, gr, game_field):
|
|
game_field[gl][gr] = 'X'
|
|
if gl == 0:
|
|
count_fields()
|
|
elif game_field[gl-1][gr] == '#':
|
|
go_right(gl, gr, game_field)
|
|
else:
|
|
gl -= 1
|
|
go_up(gl, gr, game_field)
|
|
|
|
def go_right(gl, gr, game_field):
|
|
game_field[gl][gr] = 'X'
|
|
if gr == game_field_rows-1:
|
|
count_fields()
|
|
elif game_field[gl][gr+1] == '#':
|
|
go_down(gl, gr, game_field)
|
|
else:
|
|
gr += 1
|
|
go_right(gl, gr, game_field)
|
|
|
|
def go_down(gl, gr, game_field):
|
|
game_field[gl][gr] = 'X'
|
|
if gl == game_field_lines-1:
|
|
count_fields()
|
|
elif game_field[gl+1][gr] == '#':
|
|
go_left(gl, gr, game_field)
|
|
else:
|
|
gl += 1
|
|
go_down(gl, gr, game_field)
|
|
|
|
def go_left(gl, gr, game_field):
|
|
game_field[gl][gr] = 'X'
|
|
if gr == 0:
|
|
count_fields()
|
|
elif game_field[gl][gr-1] == '#':
|
|
go_up(gl, gr, game_field)
|
|
else:
|
|
gr -= 1
|
|
go_left(gl, gr, game_field)
|
|
|
|
def count_fields():
|
|
pass
|
|
|
|
if __name__ == "__main__":
|
|
field = []
|
|
input_file = open(file, "r")
|
|
for line in input_file:
|
|
line = line.strip()
|
|
if line == "":
|
|
continue
|
|
tmp = []
|
|
for b in line:
|
|
tmp.append(b)
|
|
field.append(tmp)
|
|
input_file.close()
|
|
|
|
#print_game_field()
|
|
game_field_lines, game_field_rows = len(field), len(field[0])
|
|
gl_start, gr_start = guard_position(field)
|
|
|
|
go_up(gl_start, gr_start, field)
|
|
|
|
rec_errors = 0
|
|
for i in range(game_field_lines):
|
|
for j in range(game_field_rows):
|
|
if field[i][j] != 'X':
|
|
continue
|
|
f = deepcopy(field)
|
|
f[i][j] = "#"
|
|
try:
|
|
go_up(gl_start, gr_start, f)
|
|
except RecursionError:
|
|
rec_errors += 1
|
|
print(rec_errors)
|
|
print(f'runtime for script: {time()-start:.2f} s') |