AdventOfCode/2021/day5/day5_part1_v2.py

47 lines
1.1 KiB
Python

import numpy as np
import re
import sys
import pprint
f = open(sys.argv[1],"r")
lines=[]
for line in f:
line = line.strip('\n').split(' ')
left = [int(x) for x in line[0].split(',')]
right = [int(x) for x in line[2].split(',')]
lines.append([left,right])
grid = [[0]* 1000 for i in range(1000)]
#grid = [[0]* 10 for i in range(10)]
for line in lines:
if line[0][0] == line[1][0]: #vertical
a = min(line[0][1],line[1][1])
b = max(line[0][1],line[1][1])
for i in range(a, b+1):
grid[line[0][0]][i] += 1
elif line[0][1] == line[1][1]: #horizontal
a = min(line[0][0],line[1][0])
b = max(line[0][0],line[1][0])
for i in range(a, b+1):
grid[i][line[0][1]] += 1
else:
if line[0][0] > line[1][0]:
line = [line[1], line[0]]
for i in range(line[1][0] - line[0][0] + 1):
v = i if line[1][1] > line[0][1] else -i
h = i if line[1][0] > line[0][0] else -i
grid[line[0][0] + h] [line[0][1] + v] += 1
count = 0
for i in grid:
for j in i:
if j > 1:
count += 1
print(count)
#pprint.pprint(grid)