aboutsummaryrefslogtreecommitdiff
path: root/challenge-093/paulo-custodio/python/ch-2.py
blob: 557f0f00a4b0f9d7d8a2099f34c7dc6b0ef899e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python

# Challenge 093
#
# TASK #2 > Sum Path
# Submitted by: Mohammad S Anwar
# You are given binary tree containing numbers 0-9 only.
#
# Write a script to sum all possible paths from root to leaf.
#
# Example 1:
# Input:
#      1
#     /
#    2
#   / \
#  3   4
#
# Output: 13
# as sum two paths (1->2->3) and (1->2->4)
# Example 2:
# Input:
#      1
#     / \
#    2   3
#   /   / \
#  4   5   6
#
# Output: 26
# as sum three paths (1->2->4), (1->3->5) and (1->3->6)

import fileinput
import re

class Node:
    """one node of a tree"""
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

    def __repr__(self):
        return "Node(value: {}, left: {}, right: {})" \
                .format(self.value, self.left, self.right)

def read_input():
    """read standard input into array"""
    lines = []
    for line in fileinput.input():
        lines.append(line)
    return lines

def parse(lines):
    """parse a tree, return root Node"""

    def parse_subtree(lines, row, col):
        """parse a sub-tree, return root Node"""
        def ch(row, col):
            if row < 0 or row >= len(lines) or col < 0 or col >= len(lines[row]):
                return ' '
            else:
                return lines[row][col]

        tree = Node(int(lines[row][col]))
        if ch(row + 1, col - 1) == '/':
            tree.left = parse_subtree(lines, row + 2, col - 2)
        if ch(row + 1, col + 1) == '\\':
            tree.right = parse_subtree(lines, row + 2, col + 2)

        return tree

    found = re.search("^[ ]+\d", lines[0])
    col = found.span()[1] - 1
    return parse_subtree(lines, 0, col)

def add_tree_paths(tree):
    """sum all sub-tree paths"""

    def add_subtree_paths(node, cur_len, total_len):
        """return the subtree path len"""
        cur_len += node.value
        if node.left:
            total_len = add_subtree_paths(node.left, cur_len, total_len)
        if node.right:
            total_len = add_subtree_paths(node.right, cur_len, total_len)
        if not node.left and not node.right:        # at a leaf
            total_len += cur_len
        return total_len

    return add_subtree_paths(tree, 0, 0)

def solve():
    lines = read_input()
    tree = parse(lines)
    print(add_tree_paths(tree))

solve()