fixed some mistakes

This commit is contained in:
2024-01-04 20:04:45 +01:00
parent 04c65d31ec
commit eb2c632a03

View File

@@ -1,15 +1,15 @@
#!/usr/bin/python
sol = 0
inputfile = open("input.txt", "r")
inputfile = open("inputtest.txt", "r")
lines = inputfile.readlines()
nums = dict()
syms = dict()
nums = {}
syms = {}
for line in range(len(lines)):
tempnum = ""
tempindex = -1
linenumdict = dict()
linesymdict = dict()
linenumdict = {}
linesymdict = {}
for index in range(len(lines[line])):
tempchar = lines[line][index]
if tempchar.isdigit():
@@ -17,28 +17,28 @@ for line in range(len(lines)):
if tempindex < 0:
tempindex = index
else:
if tempnum != "" and tempindex > 0 :
if tempnum != "" and tempindex >= 0 :
linenumdict[tempindex] = tempnum
tempnum = ""
tempindex = -1
if tempchar != "." and tempchar != "\n":
if tempchar not in ('.', '\n'):
linesymdict[index] = tempchar
if tempnum != "" and tempindex > 0:
if tempnum != "" and tempindex >= 0:
linenumdict[tempindex] = tempnum
nums[line] = linenumdict
syms[line] = linesymdict
for ykey in nums:
for xkey in nums[ykey]:
hassym = False
startx = xkey
endx = xkey + len(nums[ykey][xkey])
for xkey in nums[ykey].keys():
hasNeigh= False
startx = xkey - 1
endx = xkey + len(nums[ykey][xkey]) + 1
starty = ykey - 1
endy = ykey + 1
for y in range(starty, endy):
if y in syms.keys():
if y in syms:
for x in range(startx, endx):
if x in syms[y].keys():
hassym = True
if hassym:
if x in syms[y].keys():
hasNeigh = True
if hasNeigh:
sol += int(nums[ykey][xkey])
print(sol)