Advent-of-Code/2021/5/main.py
2021-12-07 12:04:34 -05:00

74 lines
2.0 KiB
Python

lines = []
with open('data', 'r') as fp:
for line in fp:
temp = line.split(' ')
point_a = temp[0].split(',')
point_b = temp[2].split(',')
lines.append([[int(point_a[0]), int(point_a[1])], [int(point_b[0]), int(point_b[1])]])
map = {}
for line in lines:
# # DEBUG(dev):
# print line
if(line[0][0] == line[1][0]): # vertical
range_limits = [line[0][1], line[1][1]]
range_limits.sort()
range_limits[1] += 1 # inclusive
for y in range(range_limits[0], range_limits[1]):
if(tuple([line[0][0], y]) in map.keys()):
map[tuple([line[0][0], y])] = map[tuple([line[0][0], y])] + 1
else:
map[tuple([line[0][0], y])] = 1
elif(line[0][1] == line[1][1]): # horizontal
range_limits = [line[0][0], line[1][0]]
range_limits.sort()
range_limits[1] += 1 # inclusive
for x in range(range_limits[0], range_limits[1]):
if(tuple([x, line[0][1]]) in map.keys()):
map[tuple([x, line[0][1]])] = map[tuple([x, line[0][1]])] + 1
else:
map[tuple([x, line[0][1]])] = 1
# else:
# print('skip')
# # DEBUG(dev):
# min_x = 1000
# max_x = 0
# min_y = 1000
# max_y = 0
# for key in map:
# if(key[0] < min_x):
# min_x = key[0]
# elif(key[0] > max_x):
# max_x = key[0]
# if(key[1] < min_y):
# min_y = key[1]
# elif(key[1] > max_y):
# max_y = key[1]
# print(min_x, max_x)
# print(min_y, max_y)
total = 0
extra = 0
for key,value in map.items():
# # DEBUG(dev):
# print(key,value)
if(value > 1):
total = total + 1
else:
extra = extra + 1
print(total, total + extra)
# # DEBUG(dev):
# test = []
# row = []
# for x in range(0, 150):
# row.append('.')
# for y in range(0, 150):
# test.append(row)
# for key,value in map.items():
# if(key[0] < 150 and key[1] < 150):
# test[key[1]][key[0]] = value
# print(test)