solution 03p2
This commit is contained in:
51
03/03p2.py
Executable file
51
03/03p2.py
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
sol = 0
|
||||
inputfile = open("input.txt", "r")
|
||||
lines = inputfile.readlines()
|
||||
nums = {}
|
||||
gears = {}
|
||||
numindex = 0
|
||||
for line in range(len(lines)):
|
||||
tempnums = {}
|
||||
tempnum = ""
|
||||
tempindex = -1
|
||||
tempgears = {}
|
||||
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 :
|
||||
for i in range(tempindex, index):
|
||||
tempnums[i] = (numindex, tempnum)
|
||||
tempnum = ""
|
||||
tempindex = -1
|
||||
numindex += 1
|
||||
if tempchar == '*':
|
||||
tempgears[index] = (line, index)
|
||||
if tempnum != "" and tempindex >= 0:
|
||||
for i in range(tempindex, index):
|
||||
tempnums[i] = (numindex, tempnum)
|
||||
nums[line] = tempnums
|
||||
gears[line] = tempgears
|
||||
for ykey in gears:
|
||||
for xkey in gears[ykey]:
|
||||
neighlist = {}
|
||||
startx = xkey - 1
|
||||
endx = xkey + 1
|
||||
starty = ykey - 1
|
||||
endy = ykey + 1
|
||||
for y in range(starty, endy + 1):
|
||||
for x in range(startx, endx + 1):
|
||||
if y in nums and x in nums[y]:
|
||||
if nums[y][x][0] not in neighlist:
|
||||
neighlist[nums[y][x][0]] = nums[y][x][1]
|
||||
if len(neighlist) == 2:
|
||||
temp = 1
|
||||
for i in neighlist.values():
|
||||
temp *= int(i)
|
||||
sol += temp
|
||||
print(sol)
|
Reference in New Issue
Block a user