#!/usr/bin/env python
# 2021 - Advent Of Code - 5

def parse_file(file):
    horizontal_lines = []
    vertical_lines = []
    with open(file, encoding="utf-8") as f:
        for l in f.readlines():
            p1, p2 = l.strip().split(' -> ')
            strx1, stry1 = p1.split(',')
            strx2, stry2 = p2.split(',')

            # filter horizontal and vertical lines only
            if strx1 == strx2:
                p1 = (int(strx1), int(stry1))
                p2 = (int(strx2), int(stry2))
                vertical_lines.append([p1, p2])
            if stry1 == stry2:
                p1 = (int(strx1), int(stry1))
                p2 = (int(strx2), int(stry2))
                horizontal_lines.append([p1, p2])

    return horizontal_lines, vertical_lines


def draw_horizontal(g, hline):

    # print(f'drawing hline: {hline}')
    if hline[0][0] < hline[1][0]:   # left -> right
        start_point = hline[0]
        end_point = hline[1]
    else:
        start_point = hline[1]
        end_point = hline[0]

    ly = start_point[1]
    # print(f'drawing from {start_point[0]} to {end_point[0]}')
    for lx in range(start_point[0], end_point[0]+1):
        g[ly][lx] += 1


def draw_vertical(g, vline):
    # print(f'drawing vline: {vline}')
    if vline[0][1] < vline[1][1]:   # up -> down
        start_point = vline[0]
        end_point = vline[1]
    else:
        start_point = vline[1]
        end_point = vline[0]

    lx = start_point[0]
    # print(f'drawing from {start_point[0]} to {end_point[0]}')
    for ly in range(start_point[1], end_point[1]+1):
        g[ly][lx] += 1


hlines, vlines = parse_file('input.txt')
print(f'horizontal lines parsed: {len(hlines)}')
print(f'vertical lines parsed: {len(vlines)}')

# detect size of the grid (assumed 1000x1000, but why not ;-) )
maxx = 0
maxy = 0
for line in hlines + vlines:
    startp = line[0]
    endp = line[1]
    if startp[0] > maxx:
        maxx = startp[0]
    if endp[0] > maxx:
        maxx = endp[0]
    if startp[1] > maxy:
        maxy = startp[1]
    if endp[1] > maxy:
        maxy = endp[1]

print(f'detected grid size: {maxx} x {maxy}')
grid = [[0] * (maxx+1) for _ in range(maxy+1)]

# draw
for hl in hlines:
    draw_horizontal(grid, hl)

for vl in vlines:
    draw_vertical(grid, vl)

# count points
points = 0
for y in range(maxy+1):
    for x in range(maxx+1):
        if grid[y][x] > 1:
            points += 1

print(f'There\'s {points} points with at least two lines')

# bonus (or debug with the sample), draw the grid
# print('  ', end='')
# for y in range(maxy+1):
#     print(y, end='')
# print()
#
# for y in range(maxy+1):
#     print(f'{y} ', end='')
#     for x in range(maxx+1):
#         if grid[y][x] == 0:
#             print('.', end='')
#         else:
#             print(str(grid[y][x]), end='')
#    print()