solution for 02p1 and 02p2, also started with 03

This commit is contained in:
2024-01-03 01:53:36 +01:00
parent 06cec4dbc0
commit 02df576daa
4 changed files with 216 additions and 4 deletions

44
03/03p1.py Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/python
sol = 0
inputfile = open("input.txt", "r")
lines = inputfile.readlines()
nums = dict()
syms = dict()
for line in range(len(lines)):
tempnum = ""
tempindex = -1
linenumdict = dict()
linesymdict = dict()
for index in range(len(lines[line])):
tempchar = lines[line][index]
if tempchar.isdigit():
tempnum += tempchar
if tempindex < 0:
tempindex = index
else:
if tempnum != "" and tempindex > 0 :
linenumdict[tempindex] = tempnum
tempnum = ""
tempindex = -1
if tempchar != "." and tempchar != "\n":
linesymdict[index] = tempchar
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])
starty = ykey - 1
endy = ykey + 1
for y in range(starty, endy):
if y in syms.keys():
for x in range(startx, endx):
if x in syms[y].keys():
hassym = True
if hassym:
sol += int(nums[ykey][xkey])
print(sol)