Chellenge 3

Challenge 3.1


In [53]:
myinput = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input3.txt'

In [54]:
def is_triangle(sides):
    return sides[0] + sides[1] > sides[2]

In [55]:
def no_triangles(path):
    with open(path,'rt') as f:
        ntr = 0
        for line in f:
            sides = list(map(int, line.rstrip().split()))
            if is_triangle(sorted(sides)):
                ntr += 1
    return ntr

In [56]:
no_triangles(myinput)


Out[56]:
1050

Challenge 3.2


In [57]:
def no_triangles_by_cols(path):
    triangles = [[0,0,0], [0,0,0], [0,0,0]]
    with open(path,'rt') as f:
        ntr = 0
        i = 1
        for line in f:
            sides = list(map(int, line.rstrip().split()))
            for j in range(3):
                triangles[j][i % 3] = sides[j]
            if i % 3 == 0:
                for j in range(3):
                    if is_triangle(sorted(triangles[j])):
                        ntr += 1
            i += 1
    return ntr

In [58]:
no_triangles_by_cols(myinput)


Out[58]:
1921