my advent of code 2021 solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

75 lines
2.0 KiB

import sys
def aa(num):
return str(num) if num > 0 else '.'
xone = []
xtwo = []
yone = []
ytwo = []
print('Reading ', sys.argv[1])
with open(sys.argv[1], 'r') as f:
for i in f.readlines():
inp = i.strip()
temp1, temp2 = inp.split(" -> ", 1)
tempx1, tempy1 = temp1.split(",")
tempx2, tempy2 = temp2.split(",")
xone.append(int(tempx1))
xtwo.append(int(tempx2))
yone.append(int(tempy1))
ytwo.append(int(tempy2))
maxx = max(xone + xtwo)
maxy = max(yone + ytwo)
print('Max x: ', maxx, ', max y: ', maxy)
class grid:
def __init__(self, maxx, maxy):
self._points = []
for i in range(maxy + 1):
self._points.append([0] * (maxx + 1))
def mark(self, x, y):
self._points[y][x] += 1
def count(self):
counter = 0
for i in self._points:
for t in i:
if t > 1:
counter += 1
return counter
gri = grid(maxx, maxy)
for i in range(len(xone)):
if xone[i] == xtwo[i]:
poiA = min(yone[i], ytwo[i])
poiB = max(yone[i], ytwo[i])
print('Drawing y line from ', xone[i], ',', poiA, ' to ', xtwo[i], ',', poiB + 1)
for t in range(poiA, poiB + 1):
gri.mark(xone[i], t)
elif yone[i] == ytwo[i]:
poiA = min(xone[i], xtwo[i])
poiB = max(xone[i], xtwo[i])
print('Drawing x line from ', poiA, ',', yone[i], ' to ', poiB + 1, ',', ytwo[i])
for t in range(poiA, poiB + 1):
gri.mark(t, yone[i])
else:
xdir = -1 if xone[i] > xtwo[i] else 1
ydir = -1 if yone[i] > ytwo[i] else 1
xcoords = list(range(xone[i], xtwo[i] + xdir, xdir))
ycoords = list(range(yone[i], ytwo[i] + ydir, ydir))
#print(ycoords)
#print(xcoords)
for x, y in zip(xcoords, ycoords):
gri.mark(x, y)
# Print the grid as ASCII art.
for p in gri._points:
print(''.join([aa(x) for x in p]))
solu = gri.count()
print(solu, ' intersections found')