This commit is contained in:
Tyrel Souza 2022-12-18 01:36:15 -05:00
parent 50d0429fc4
commit 6c482630c2
3 changed files with 93 additions and 24 deletions

View File

@ -4,7 +4,7 @@ import matrix
import shared
import scanf
from dataclasses import dataclass
from collections import defaultdict
from collections import defaultdict, deque
from typing import List, Dict
from pprint import pprint
import networkx as nx
@ -89,7 +89,7 @@ class Part1:
if valve.opened_at >= 0:
pressure += valve.rate
opened.append(valve.label)
print(f"== Min {minute}:: {len(opened)} Valves {', '.join(opened)} are open, releasing {pressure} pressure")
print(f"== Min {minute+1}:: {len(opened)} Valves {', '.join(opened)} are open, releasing {pressure} pressure")
def calculate_total_flow(self):
total = 0
@ -98,7 +98,46 @@ class Part1:
total += valve.rate * (30 - valve.opened_at)
return total
def run(self):
paths = defaultdict(lambda: -1)
# lbl, flow, time_left, visited
q = deque([('AA', 0, self.minutes, set())])
x = -1
while q:
x+=1
# get recent nodes
pos, flow, minutes_left, cur_path = q.popleft()
# find who we can reach in time
reachable = [n for n in self.path_distances[pos]
if n not in cur_path # not visited
and self.path_distances[pos][n] < minutes_left]
hashable = frozenset(cur_path)
if paths[hashable] < flow:
paths[hashable] = flow
for r in reachable:
d = self.path_distances[pos][r]
r_flow = (minutes_left - d - 1) * self.valves[r].rate
# add neighbor
cur_path.add(r)
q.append((r, flow + r_flow, minutes_left - d -1, cur_path))
print("added",x,r)
print(paths.values())
print(max(paths.values()))
def _run(self):
# Construct the graph with vertices & edges from the input
# Call a function to compute the distances between every pair of vertices
# Create a closed set containing all the valves with non-zero rates
@ -127,7 +166,7 @@ class Part1:
if remaining <= 0:
print("ran out of time")
break
self.do_tick(31-remaining)
self.do_tick(30-remaining)
# CALCULATE PRIORITIES
pris = priority(remaining)

View File

@ -121,7 +121,7 @@ def part1(rows):
rock = 0
shapes = []
spawning = True
while rock < 2022: #1_000_000_000_000:
while rock < 2022:
if spawning:
# Add last rock's coords to all coords
if shapes:
@ -183,7 +183,7 @@ def render(width, height, shapes):
# @shared.profile
def part2(rows):
pass
print("NAH BRO")
def main():

View File

@ -1,8 +1,6 @@
import matrix
import shared
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from pprint import pprint
from dataclasses import dataclass
from typing import Set, Tuple
from functools import cached_property
@ -43,26 +41,59 @@ def count_neighbor_coords(x,y,z):
# @shared.profile
def part1(rows):
cubes = []
for row in rows:
x,y,z = row
cube = Cube(x=x,y=y,z=z)
cube.set_neighbor_coords()
cubes.append(cube)
cubes = {}
for idx, (x,y,z) in enumerate(rows):
cubes[(x,y,z)] = {'n':get_neighbors(x,y,z), 'not_me': tuple(map(tuple,rows[:idx] + rows[idx+1:]))}
potential = len(cubes) * 6
for idx in range(len(cubes)):
cube = cubes[idx]
for other in cubes[:idx] + cubes[idx+1:]:
if other.xyz in cube.neighbor_coords:
for coords, cube in cubes.items():
for other in cube['not_me']:
if other in cube['n']:
potential -= 1
print(potential)
# @shared.profile
def part2(rows):
cubes = []
cubes = {}
maxX,maxY,maxZ = 0,0,0
for idx, (x,y,z) in enumerate(rows):
cubes[(x,y,z)] = {'n':get_neighbors(x,y,z), 'not_me': set(map(tuple,rows[:idx] + rows[idx+1:]))}
maxX = max(maxX,x)
maxY = max(maxY,y)
maxZ = max(maxZ,z)
_cubes = frozenset(cubes.keys())
potential = len(cubes) * 6
for coords, cube in cubes.items():
for other in cube['not_me']:
if other in cube['n']:
potential -= 1
air = set()
for x in range(0,maxX+1):
for y in range(0,maxY+1):
for z in range(0,maxZ+1):
air.add((x,y,z))
air -= _cubes # Remove all lava from air
for a in air:
neighbors = get_neighbors(*a)
if len(neighbors & _cubes)==6:
#print(a, "====", neighbors & _cubes)
potential -= 6
# _ns = get_neighbors(x,y,z)
# lava_neighbors = _ns & _cubes
# if len(lava_neighbors) == 6:
# potential -= 6
print(potential)
# @shared.profile
def _part2(rows):
cubes = []
_lava = set()
for row in rows:
@ -94,7 +125,6 @@ def part2(rows):
continue
# is air
_ns = get_neighbors(x,y,z)
air_touching_lava = False
for _n in _ns:
if _n in _lava:
@ -113,11 +143,11 @@ def part2(rows):
def main():
rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
rows = [list(map(int,row.split(","))) for row in shared.load_rows(18)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
#rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())