AdventOfCode/2023/day11/part1.py

76 lines
1.7 KiB
Python

import sys
import os
from pprint import pprint
import time
import math
#colors
from termcolor import colored
grid = []
def p(x,*args):
for idx,i in enumerate(x):
for jdx,j in enumerate(i):
if j == '#':
print(colored(j,'red'),end='')
#elif (idx,jdx) in steps:
# print(colored(j,'green'),end='')
else:
print(j,end='')
print()
with open(sys.argv[1]) as file:
for line in file:
grid.append(list(line.rstrip()))
steps = []
ngrid = []
def expand_galaxy(grid):
ngrid=[]
for r,row in enumerate(grid):
if not '#' in row:
ngrid.append(row)
ngrid.append(row)
return ngrid
def rotate_galaxy(grid,times):
for i in range(times):
grid = list(zip(*grid[::-1]))
return grid
p(grid)
grid = expand_galaxy(grid)
grid = rotate_galaxy(grid,1)
grid = expand_galaxy(grid)
grid = rotate_galaxy(grid,3)
p(grid)
galaxies = []
for r,row in enumerate(grid):
for c,col in enumerate(row):
if grid[r][c] == '#':
galaxies.append((r,c))
count = 0
checked = 0
skip = []
for gdx,g in enumerate(galaxies):
for pdx,p in enumerate(galaxies):
if g != p and (g,p) not in skip:
print(checked,end='')
print(' of ',end='')
print(len(galaxies)*len(galaxies)/2)
skip.append((g,p))
skip.append((p,g))
checked += 1
d = abs(list(g)[0] - list(p)[0]) + abs(list(g)[1]-list(p)[1])
#d = math.dist(g,p)
count += d
#print('Distance between galaxy ' + str(gdx+1) + ' ' + str(g) + ' and ' + str(pdx+1) + ' ' + str(p) + ' is ' + str(d))
print(checked)
print(count)