From fde97f37c926f6fea38d6c62e4528e0f26ee53ea Mon Sep 17 00:00:00 2001 From: tebarius Date: Fri, 6 Dec 2024 12:40:53 +0100 Subject: [PATCH] 06-2 --- 06/06-2.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 06/06-2.py diff --git a/06/06-2.py b/06/06-2.py new file mode 100644 index 0000000..e71f85e --- /dev/null +++ b/06/06-2.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +file = "./input.txt" +# file = "./ex.txt" +from copy import deepcopy +import sys +sys.setrecursionlimit(10000) +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) + +rec_errors = 0 +for i in range(game_field_lines): + for j in range(game_field_rows): + 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') \ No newline at end of file