Advent-of-Code/2021/5/main2.py

112 lines
3.2 KiB
Python
Raw Permalink Normal View History

2021-12-07 12:04:34 -05:00
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: # per spec, must be 45 degree
amount = abs(line[0][0] - line[1][0]) + 1
values = []
for i in range(0, amount):
values.append([0,0])
range_limits_x = [line[0][0], line[1][0]]
# make inclusive in correct direction:
step = 1
if(range_limits_x[1] < range_limits_x[0]):
step = -1
range_limits_x[1] -= 1
else:
range_limits_x[1] += 1
i = 0
for x in range(range_limits_x[0], range_limits_x[1], step):
values[i][0] = x
i = i + 1
range_limits_y = [line[0][1], line[1][1]]
# make inclusive in correct direction:
step = 1
if(range_limits_y[1] < range_limits_y[0]):
step = -1
range_limits_y[1] -= 1
else:
range_limits_y[1] += 1
i = 0
for y in range(range_limits_y[0], range_limits_y[1], step):
values[i][1] = y
i = i + 1
# # DEBUG(dev):
# print('RANGES', range_limits_x, range_limits_y)
# print(values)
for value in values:
if(tuple(value) in map.keys()):
map[tuple(value)] = map[tuple(value)] + 1
else:
map[tuple(value)] = 1
# # 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)