From 3b7295f5c66551cb8144c393b66773a4adbc9d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C5=A1per=20Dobrovoljc?= Date: Tue, 26 Dec 2023 20:37:48 +0100 Subject: [PATCH] 09_kontrola - ocena 7-9 --- 09_kontrola/naloga.py | 125 +++++++++++++++++++++++++++++------- 09_kontrola/naloga_debug.py | 91 ++++++++++++++++++++++++++ 09_kontrola/test.txt | 30 +++++++++ 3 files changed, 222 insertions(+), 24 deletions(-) create mode 100644 09_kontrola/naloga_debug.py create mode 100644 09_kontrola/test.txt diff --git a/09_kontrola/naloga.py b/09_kontrola/naloga.py index 5934187..df731f5 100644 --- a/09_kontrola/naloga.py +++ b/09_kontrola/naloga.py @@ -2,6 +2,7 @@ from itertools import groupby import operator from os import path from collections import Counter +import re def preberi_zemljevid(ime_dat: str): @@ -10,41 +11,49 @@ def preberi_zemljevid(ime_dat: str): return file.read().strip().split("\n") -def check(x: int, y: int, c: str, zemljevid: list[str]): - return x >= 0 and y >= 0 and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c) +def check(x: int, y: int, c: str, zemljevid: list[str], prepovedani: set = None): + if prepovedani is None: + prepovedani = set() + + return ( + (x, y) not in prepovedani + and 0 <= y < len(zemljevid) + and 0 <= x < len(zemljevid[0]) + and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c) + ) -def najblizji(x: int, y: int, c: str, zemljevid: list[str]): +def najblizji_distance(x: int, y: int, c: str, zemljevid: list[str], prepovedani: set): d = 1 - max_d = max(len(zemljevid), len(zemljevid[0])) + max_d = len(zemljevid) + len(zemljevid[0]) while d <= max_d: x -= 1 - for i in range(d): - if check(x, y, c, zemljevid): - return x, y - x += 1 - y += 1 - for i in range(d): - if check(x, y, c, zemljevid): - return x, y - x += 1 - y -= 1 - for i in range(d): - if check(x, y, c, zemljevid): - return x, y - x -= 1 - y -= 1 - for i in range(d): - if check(x, y, c, zemljevid): - return x, y - x -= 1 - y += 1 + ry = 0 + ry_inc = 1 + + for rx in range(d * 2 + 1): + if check(x + rx, y - ry, c, zemljevid, prepovedani): + return x + rx, y - ry, d + if check(x + rx, y + ry, c, zemljevid, prepovedani): + return x + rx, y + ry, d + if ry == d: + ry_inc = -1 + ry += ry_inc d += 1 +def najblizji(x: int, y: int, c: str, zemljevid: list[str], prepovedani: set = None): + if prepovedani is None: + prepovedani = set() + res = najblizji_distance(x, y, c, zemljevid, prepovedani) + if res is None: + return + return res[0], res[1] + + def najpogostejsi(x, y, d, zemljevid): chars = [] for y2, line in enumerate(zemljevid): @@ -75,3 +84,71 @@ def vsi_najpogostejsi(x, y, d, zemljevid): _, c = max(sorted(a)) return set(k for (k, v) in c) + + +def angelca(x, y, znamenitosti, zemljevid): + prepovedani = set() + distance = 0 + for znam in znamenitosti: + res = najblizji_distance(x, y, znam, zemljevid, prepovedani) + if res is None: + return distance + prepovedani.add((x, y)) + x, y, d = res + distance += d + + return distance + + +def johanca(x, y, pot, zemljevid): + znamenitosti = [] + obiskane = set() + + for match in re.finditer("([0-9]*)([<>v^])", pot): + dist, dir = match.groups() + dist = int(dist) if dist != "" else 1 + + match dir: + case "^": + y -= dist + case "v": + y += dist + case "<": + x -= dist + case ">": + x += dist + + if ( + (x, y) not in obiskane + and 0 <= y < len(zemljevid) + and 0 <= x < len(zemljevid[0]) + and zemljevid[y][x] != "." + ): + obiskane.add((x, y)) + znamenitosti.append(zemljevid[y][x]) + + return "".join(znamenitosti) + + +def najboljsa_cetrt(a: int, zemljevid: list[str]): + x_len = len(zemljevid[0]) - a + y_len = len(zemljevid) - a + y_len = y_len if y_len > 0 else 1 + + max_count = 0 + max_x = 0 + max_y = 0 + + for x in range(x_len): + for y in range(y_len): + count = 0 + for ry in range(a): + for rx in range(a): + if zemljevid[y + ry][x + rx] != ".": + count += 1 + if count > max_count: + max_count = count + max_x = x + max_y = y + + return max_x, max_y diff --git a/09_kontrola/naloga_debug.py b/09_kontrola/naloga_debug.py new file mode 100644 index 0000000..9267eaa --- /dev/null +++ b/09_kontrola/naloga_debug.py @@ -0,0 +1,91 @@ +import time +from os import path, system + + +def preberi_zemljevid(ime_dat: str): + directory = path.dirname(path.abspath(__file__)) + file = open(path.join(directory, ime_dat)) + return file.read().strip().split("\n") + + +z = [list(" " + c for c in line) for line in preberi_zemljevid("test.txt")] + + +def print_z(): + system('clear') + for line in z: + print("".join(line)) + + +def check(x: int, y: int, c: str, zemljevid: list[list[str]], prepovedani: set = None): + if prepovedani is None: + prepovedani = set() + + return ( + (x, y) not in prepovedani + and 0 <= y < len(zemljevid) + and 0 <= x < len(zemljevid[0]) + and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c) + ) + + +counter = 1 + + +def najblizji_distance(x: int, y: int, c: str, zemljevid: list[list[str]], prepovedani: set): + global counter + + z[y][x] = " ." + + d = 1 + max_d = len(zemljevid) + len(zemljevid[0]) + + while d <= max_d: + x -= 1 + + ry = 0 + ry_inc = 1 + + for rx in range(d * 2 + 1): + if (0 <= y - ry < len(zemljevid) + and 0 <= x + rx < len(zemljevid[0])): + z[y - ry][x + rx] = (" " * (5 - len(str(d)))) + str(d) + + print_z() + counter += 1 + + if (0 <= y + ry < len(zemljevid) + and 0 <= x + rx < len(zemljevid[0])): + z[y + ry][x + rx] = (" " * (5 - len(str(d)))) + str(d) + + print_z() + counter += 1 + + if check(x + rx, y + ry, c, zemljevid, prepovedani): + return x + rx, y + ry, d + if check(x + rx, y - ry, c, zemljevid, prepovedani): + return x + rx, y - ry, d + if ry == d: + ry_inc = -1 + ry += ry_inc + + d += 1 + + +def angelca(x, y, znamenitosti, zemljevid): + prepovedani = set() + distance = 0 + for znam in znamenitosti: + res = najblizji_distance(x, y, znam, zemljevid, prepovedani) + if res is None: + return distance + x, y, d = res + prepovedani.add((x, y)) + distance += d + + return distance + + +res = najblizji_distance(37, 14, "c", z, set()) + +print(res) diff --git a/09_kontrola/test.txt b/09_kontrola/test.txt new file mode 100644 index 0000000..e2f5331 --- /dev/null +++ b/09_kontrola/test.txt @@ -0,0 +1,30 @@ +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +......................................... +..c...................................... +......................................... +.........................................