2016 - Day 3 (Squares With Three Sides)

View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3

#Advent of Code
#2016 Day 3, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    possible = 0

    for t in puzzle_input:
        if t == "": continue
        values = t.split(" ")
        sides = []
        for v in values:
            if v != "":
                sides.append(v)
        if (int(sides[0]) + int(sides[1]) > int(sides[2]) and
            int(sides[1]) + int(sides[2]) > int(sides[0]) and
            int(sides[2]) + int(sides[0]) > int(sides[1])):
            possible += 1

    return possible

def main():
    puzzle_input = util.read.as_lines()

    possible = solve(puzzle_input)

    print("There are " + str(possible) + " possible triangles.")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

if __name__ == "__main__":
    run(main)
Part 2
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
#!/usr/bin/env python3

#Advent of Code
#2016 Day 3, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    possible = 0
    column1, column2, column3 = [], [], []

    for t in puzzle_input:
        if t == "": continue
        values = t.split(" ")
        sides = []
        for v in values:
            if v != "":
                sides.append(v)
        column1.append(str(sides[0]).zfill(3))
        column2.append(str(sides[1]).zfill(3))
        column3.append(str(sides[2]).zfill(3))

    for i in range(0, len(column1), 3):
        a, b, c = column1[i:i + 3]
        if (int(a) + int(b) > int(c) and
            int(b) + int(c) > int(a) and
            int(c) + int(a) > int(b)):
            possible += 1
        a, b, c = column2[i:i + 3]
        if (int(a) + int(b) > int(c) and
            int(b) + int(c) > int(a) and
            int(c) + int(a) > int(b)):
            possible += 1
        a, b, c = column3[i:i + 3]
        if (int(a) + int(b) > int(c) and
            int(b) + int(c) > int(a) and
            int(c) + int(a) > int(b)):
            possible += 1

    return possible

def main():
    puzzle_input = util.read.as_lines()

    possible = solve(puzzle_input)

    print("There are " + str(possible) + " possible triangles.")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

if __name__ == "__main__":
    run(main)
Return to solution index