<jupyter_start><jupyter_text>Day 3: Squares With Three Sidesauthor: <NAME>license: [MIT](https://opensource.org/licenses/MIT)[link to problem statement](http://adventofcode.com/2016/day/3) Now that you can think clearly, you move deeper into the labyrinth of hallways and office furniture that makes up this part of Easter Bunny HQ. This must be a graphic design department; the walls are covered in specifications for triangles.Or are they?The design document gives the side lengths of each triangle it describes, but... `5 10 25`? Some of these aren't triangles. You can't help but mark the impossible ones.In a valid triangle, the sum of any two sides must be larger than the remaining side. For example, the "triangle" given above is impossible, because `5 + 10` is not larger than `25`.In your puzzle input, how many of the listed triangles are possible?  Solution logicOn the face of it, this is a very trivial problem. Simply check whether the sum is larger than the remaining side. If it is, add one to the counter, if it is not, then don't. The tricky part here is that the inputs are not ordered, so we need to sort them first, and then take the smaller of the two sides. Trivial! Algorithm    - set `number_of_triangles` to `0`    - for each line of input:        - tokenize based on whitespace        - sort the sequence into `a, b, c` as `ints`        - check if `a + b > c`        - if it is, increment counter First, read in the input<jupyter_code>with open('../inputs/day03.txt', 'r') as f:
    data = f.readlines()<jupyter_output><empty_output><jupyter_text>Initialize the counter<jupyter_code>number_of_triangles = 0<jupyter_output><empty_output><jupyter_text>Tokenize the input, then sort the tokens.<jupyter_code>tokenized_data = [
    [int(n) for n in line.split()]
    for line in data]
sorted_data = [sorted(line) for line in tokenized_data]
for a, b, c in sorted_data:
    if a + b > c:
        number_of_triangles += 1<jupyter_output><empty_output><jupyter_text>The answer is in `number_of_triangles`.  Part TwoNow that you've helpfully marked up their design documents, it occurs to you that triangles are specified in groups of three vertically. Each set of three numbers in a column specifies a triangle. Rows are unrelated.For example, given the following specification, numbers with the same hundreds digit would be part of the same triangle:    101 301 501    102 302 502    103 303 503    201 401 601    202 402 602    203 403 603In your puzzle input, and instead reading by columns, how many of the listed triangles are possible?  Solution LogicOkay, this is tricky than the first one. So rows are irrelevant, and the columns contain the three vertices. Now the question arises, whether the numbers are to be taken consecutively or in sets of three? It is ambigious. For now, I'm assuming that the numbers are in fact, to be taken one after the other, sort of like the classic substring problem.So we take three rows at a time, using an index and a `for` loop, and then iterate until the end of data, checking whether there are any triangles in the three sets of inputs. The last index will be `length of data - 2`.<jupyter_code>number_of_triangles = 0
for index in range(0, len(tokenized_data) - 2, 3):
    rows = [
        sorted([tokenized_data[row][item] for row in range(index, index + 3)])
        for item in range(0, 3)]
    for a, b, c in rows:
        if a + b > c:
            number_of_triangles += 1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06877697841726618, 'fraction_numerical': 0.024172661870503598, 'mean_word_length': 3.9515669515669516, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '330129', 'license': 'MIT', 'n_tokens_mistral': 1046, 'n_tokens_neox': 953, 'n_words': 503, 'path': '2016/python3/Day03.ipynb', 'repo_name': 'coolharsh55/advent-of-code', 'size': '3488'}