opti: day03 : part 1

This commit is contained in:
starnakin 2023-12-03 09:45:12 +01:00
parent feb5cdd844
commit 95320ae0b4

View File

@ -2,42 +2,28 @@ text: str = open("input.txt", "r").read()
value: int = 0; value: int = 0;
def get_symbol_around(list:[str], pos: tuple, length: int): def is_partnumber(lst:[str], x, y, length: int):
start = [0,0] start_x = x - (x > 0)
end = [0,0] start_y = y - (y > 0)
stop_x = x + length + (x < len(lst[y]) - 1)
start[0] = pos[0] stop_y = y + (y < len(lst) - 1)
start[1] = pos[1] for y, line in enumerate(lst[start_y:stop_y + 1]):
start[0] -= (start[0] != 0) line = line[start_x:stop_x]
start[1] -= (start[1] != 0) for x, char in enumerate(line):
end[0] = pos[0] if (not char.isdigit() and not char == "."):
end[1] = pos[1]
end[0] += length
end[0] += end[0] < len(list[pos[1]])
end[1] += end[1] < len(list)
bozo = list[start[1]:end[1] + 1]
for y in bozo:
bozo2 = y[start[0]:end[0]]
print(bozo2)
for x in bozo2:
if (not x.isdigit() and not x == "."):
print()
return 1 return 1
print()
return 0 return 0
lst = text.split("\n") lst = text.split("\n")
end: int = 0 length: int = 0
for y in range(len(lst)): for y in range(len(lst)):
lst[y] += "." lst[y] += "."
for x in range(len(lst[y])): for x in range(len(lst[y])):
if (lst[y][x].isdigit()): if (lst[y][x].isdigit()):
end += 1; length += 1
elif (end != 0): elif (length != 0):
if (get_symbol_around(lst, (x - end, y), end)): if (is_partnumber(lst, x - length, y, length)):
value += int(lst[y][x - end:x]) value += int(lst[y][x - length:x])
else: length = 0
print(f"./input.txt:{y + 1}:{x}")
end = 0;
print(value) print(value)