John Lekberg


JUNTO Practice - "Project Euler Problem 102"

Discussed on 2019-02-18.

Project Euler Problem 102


Solution - Daniel

"""Project Euler 102"""

_author_ = "Daniel Bassett"

import math

total = 0

for i, line in enumerate(open("p102_triangles.txt", "r")):
    xa, ya, xb, yb, xc, yc = map(int, line.split(","))
    point_a = xa * yb - ya * xb > 0
    point_b = xb * yc - yb * xc > 0
    point_c = xc * ya - yc * xa > 0

    total += point_a == point_b == point_c

print(total)

Solution - John

"""Project Euler 102"""

__author__ = "John Lekberg"

count = 0

with open("p102_triangles.txt", "r") as triangle_file:
    for line in triangle_file:
        x1, y1, x2, y2, x3, y3 = map(int, line.split(","))
        whole_area = abs(
            (x1 - x3) * (y2 - y1) - (x1 - x2) * (y3 - y1)
        )
        fragment_area = (
            abs(x1 * y2 - x2 * y1)
            + abs(x1 * y3 - x3 * y1)
            + abs(x2 * y3 - x3 * y2)
        )
        if whole_area == fragment_area:
            count += 1

print(count)

Solution - Oscar

import numpy as np


def has_origin(coord_arr):
    v1 = coord_arr[:2]
    v2 = coord_arr[2:4]
    v3 = coord_arr[4:]

    try:
        alpha = (
            ((v2[1] - v3[1]) * (0 - v3[0]))
            + ((v3[0] - v2[0]) * (0 - v3[1]))
        ) / (
            ((v2[1] - v3[1]) * (v1[0] - v3[0]))
            + ((v3[0] - v2[0]) * (v1[1] - v3[1]))
        )

        beta = (
            ((v3[1] - v1[1]) * (0 - v3[0]))
            + ((v1[0] - v3[0]) * (0 - v3[1]))
        ) / (
            ((v2[1] - v3[1]) * (v1[0] - v3[0]))
            + ((v3[0] - v2[0]) * (v1[1] - v3[1]))
        )

        gamma = 1 - alpha - beta

    except ZeroDivisionError:
        return print("Not a triangle")

    return (
        False
        if True
        in [not (0 <= i <= 1) for i in [alpha, beta, gamma]]
        else True
    )


triangles = np.loadtxt(
    "p102_triangles.txt", dtype=np.int32, delimiter=","
)
origin_triangles = 0

for i in range(1000):
    if has_origin(triangles[i]):
        origin_triangles += 1
print(origin_triangles)