Day 3: Squares With Three Sides

author: Harshvardhan Pandit

license: MIT

link to problem statement

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 logic

On 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


In [1]:
with open('../inputs/day03.txt', 'r') as f:
    data = f.readlines()

Initialize the counter


In [2]:
number_of_triangles = 0

Tokenize the input, then sort the tokens.


In [3]:
tokenized_data = [
    [int(n) for n in line.split()]
    for line in data]
sorted_data = [sorted(line) for line in tokenized_data]

In [4]:
for a, b, c in sorted_data:
    if a + b > c:
        number_of_triangles += 1

The answer is in number_of_triangles.

Part Two

Now 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 603

In your puzzle input, and instead reading by columns, how many of the listed triangles are possible?

Solution Logic

Okay, 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.


In [5]:
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

Answer is in number_of_triangles

== END ==