AdventOfCode/2021/day5/day5_part1.py

61 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-11-02 13:12:47 +01:00
import numpy as np
import re
import sys
f = open(sys.argv[1],"r")
cords=[]
llist=[]
for line in f:
if re.search("^[0-9]",line.rstrip()):
for i in line.rstrip().split(' -> '):
llist.append(i.rstrip().split(','))
max_x = 0
max_y = 0
for i in llist:
if int(i[0]) > max_x:
max_x = int(i[0])
if int(i[1]) > max_y:
max_y = int(i[1])
#field = np.zeros((int(max_y)+1,int(max_x)+1))
field = np.zeros((1000,1000))
for i in range(0,len(llist)-1,2):
x = int(llist[i][0])
y = int(llist[i][1])
X = int(llist[i+1][0])
Y = int(llist[i+1][1])
#print('Compare ' + str(x)+','+str(y)+' and '+ str(X) + ',' + str(Y))
if x != X and y == Y:
if x > X:
temp = x
x = X
X = temp
print('X: Going from ' + str(x) + ' to ' + str(X))
for j in range(x,X+1):
field[y][j] += 1
#print(field)
if y != Y and x == X:
print('Y: Going from ' + str(y) + ' to ' + str(Y))
if y > Y:
temp = y
y = Y
y = temp
for j in range(y,Y+1):
field[j][x] += 1
#print(field)
count = 0
for i in field:
for j in i:
if int(j) >= 2: count+=1
print(count)