Puzzle #3

 9th July 2023 at 11:32am

First part

Finally, something a little easier.

with open("input") as file_input:
    list_of_strings = list(map(lambda x: x.strip().split(), file_input.read().strip().splitlines()))
    list_of_ints = [[int(x) for x in sublist] for sublist in list_of_strings]

res = list(filter(lambda l: \
        l[0] + l[1] > l[2] and \
        l[0] + l[2] > l[1] and \
        l[1] + l[2] > l[0], list_of_ints))

print(len(res))

I'm not entirely satisfied with the solution because it is not easily adapted to n sides (instead of 3); but it works.


Second part

For the second part, just a slight rework of the input should do it. It is akin to producing 3x3 matrices throughout the whole input, and then taking the transpose.

I didn't think too long on a solution; I did some googling on how to transpose a matrix in Python, which gave me the nice zip(*l) trick; this would, however, produced nested 3x3 matrices inside a list (thus, a list of lists of lists), which would imply changing the later code to reflect another nesting layer.

from itertools import chain

with open("input") as file_input:
    list_of_strings = list(map(lambda x: x.strip().split(), \
            file_input.read().strip().splitlines()))
    list_of_ints = [
            [int(x) for x in sublist] \
            for sublist in list_of_strings
            ]
    transposed = [
            [list(i) for i in zip(*list_of_ints[j:j + 3])] \
            for j in range(0, len(list_of_ints), 3)
            ]
    flattened = list(chain(*transposed))

res = list(filter(lambda l: \
        l[0] + l[1] > l[2] and \
        l[0] + l[2] > l[1] and \
        l[1] + l[2] > l[0], flattened))

print(len(res))