advent_of_code/2023/day22/main.py

147 lines
4.3 KiB
Python

from dis import Instruction
import re
from tabnanny import check
import numpy as np
f = open('input.txt', 'r')
# f = open('test.txt', 'r')
content = f.read()
lines = content.splitlines()
blocks = []
for line in lines:
temp = line.split("~")
start_coor = [int(x) for x in temp[0].split(",")]
end_coor = [int(x) for x in temp[1].split(",")]
mismatch_count = 0
for i in range(3):
if start_coor[i] != end_coor[i]:
mismatch_count += 1
if mismatch_count > 1:
print(mismatch_count)
blocks.append([start_coor, end_coor])
blocks.sort(key = lambda x: x[0][2])
def move_down_until_collision(i, j, blocks):
collide = True
for k in range(2):
min_i = min(blocks[i][0][k], blocks[i][1][k])
max_i = max(blocks[i][0][k], blocks[i][1][k])
min_j = min(blocks[j][0][k], blocks[j][1][k])
max_j = max(blocks[j][0][k], blocks[j][1][k])
# # for cur_blocks in [min_i, max_i]:
# for other_blocks in [min_j, max_j]:
# if other_blocks < min_i or other_blocks > max_i:
# print(f"no_collision between {i} and {j}")
# collide = False
# break
if collide:
return blocks[i][0][2] - blocks[j][1][2]
else:
return blocks[i][0][2]
num_holding_up = []#[[]]*len(blocks)
being_held_by = []#[0]*len(blocks)
def intersect(line1, line2):
col_x = False
col_y = False
for i in range(line1[0][0], line1[1][0]+1):
if i >= line2[0][0] and i <= line2[1][0]:
col_x = True
break
for i in range(line1[0][1], line1[1][1]+1):
if i >= line2[0][1] and i <= line2[1][1]:
col_y = True
break
if col_x and col_y:
return True
else:
return False
for i in range(len(blocks)):
num_holding_up.append([])
being_held_by.append([])
move_z = 1
collide = False
while not collide:
collision_count = 0
for j in range(0, i):
if blocks[j][1][2] == blocks[i][0][2]-1:
# for k in range(2):
# idx_j = 1 if k == 0 else 0
# min_i = min(blocks[i][0][k], blocks[i][1][k])
# max_i = max(blocks[i][0][k], blocks[i][1][k])
# min_j = min(blocks[j][0][idx_j], blocks[j][1][idx_j])
# max_j = max(blocks[j][0][idx_j], blocks[j][1][idx_j])
# min_i = blocks[i][1][k]
# max_i = blocks[i][0][k]
# min_j = blocks[j][0][idx_j]
# max_j = blocks[j][1][idx_j]
# if (max_i >= min_j and min_i < min_j) or (max_i >= max_j and min_i < max_j ) or (max_i == min_j and min_j == min_i):
if intersect(blocks[j], blocks[i]):
num_holding_up[j].append(i)
being_held_by[i].append(j)
collision_count +=1
collide = True
# break
print(collision_count)
if collide or blocks[i][1][2] == 1:
print("Potential collision")
collide = True
else:
for num_blocks in range(2):
blocks[i][num_blocks][2] -= move_z
# if i == 0:
# move_z = blocks[i][0][2]
# for num_blocks in range(2):
# blocks[i][num_blocks][2] -= move_z
# else:
# for j in range(0, i):
# if blocks[j][1][2] == blocks[i][0][2]-1:
# print("Potential collision")
# # new_z = move_down_until_collision(i, j, blocks)
# # print("new_z", new_z)
# # if new_z < move_z:
# # move_z = new_z
# print(move_z)
# for num_blocks in range(2):
# blocks[i][num_blocks][2] -= move_z
print(blocks)
print(num_holding_up)
print(being_held_by)
sum = 0
for count, i in enumerate(num_holding_up):
if len(i) == 0:
sum+=1
elif len(i) > 0:
removable = True
for j in i:
if len(being_held_by[j]) <= 1:
removable = False
break
if removable:
print(f"{count} is removable")
sum+=1
print(sum)
f.close()