diff --git a/17.py b/17.py old mode 100644 new mode 100755 index f8c28ce..f8c28ce --- a/17.py +++ b/17.py diff --git a/18.data b/18.data new file mode 100644 index 0000000..e236c2f --- /dev/null +++ b/18.data @@ -0,0 +1,15 @@ +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 diff --git a/18.py b/18.py new file mode 100755 index 0000000..749a983 --- /dev/null +++ b/18.py @@ -0,0 +1,80 @@ +#!/usr/bin/python + +import sys +import pdb + +class Node: + parentNodes = [] + childNodes = [] + weight = 0 + value = 0 + + def __init__(self,value): + self.value = value + + def calcWeight(self): + maxWeight = 0 + for node in self.parentNodes: + weight = node.weight + node.value + if(weight > maxWeight): + maxWeight = weight + self.weight = maxWeight + + def __str__(self): + return str(self.value) + + +targetNode = None + +nodeMap = [] + +def buildNodeMap(nodeMap): + for i in range(0,len(nodeMap)): + for j in range(0,len(nodeMap[i])): + node = nodeMap[i][j] + + if(i != len(nodeMap)-1): + node.childNodes.append(nodeMap[i+1][j]) + node.childNodes.append(nodeMap[i+1][j+1]) + + if(i != 0): + if(j == 0): + node.parentNodes.append(nodeMap[i-1][j]) + elif(j == len(nodeMap[i])-1): + node.parentNodes.append(nodeMap[i-1][j-1]) + else: + node.parentNodes.append(nodeMap[i-1][j]) + node.parentNodes.append(nodeMap[i-1][j-1]) + + +def dijkstraReverse(nodeMap): + for row in nodeMap: + for node in row: + node.calcWeight() + +numRow = 0 +for line in sys.stdin: + nodeMap.append([]) + for elem in line.split(): + nodeMap[numRow].append(Node(int(elem))) + numRow += 1 + +pdb.set_trace() + +buildNodeMap(nodeMap) +nodeMap.append([]) +targetNode = Node(0) +for node in nodeMap[-2]: + targetNode.parentNodes.append(node) + +nodeMap[-1].append(targetNode) + +for row in nodeMap: + string = "" + for node in row: + string += str(node) + " " + print string + +dijkstraReverse(nodeMap) + +print "Result is: " + str(targetNode.weight)