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 shared
import scanf import scanf
from dataclasses import dataclass from dataclasses import dataclass
from collections import defaultdict from collections import defaultdict, deque
from typing import List, Dict from typing import List, Dict
from pprint import pprint from pprint import pprint
import networkx as nx import networkx as nx
@ -89,7 +89,7 @@ class Part1:
if valve.opened_at >= 0: if valve.opened_at >= 0:
pressure += valve.rate pressure += valve.rate
opened.append(valve.label) 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): def calculate_total_flow(self):
total = 0 total = 0
@ -98,7 +98,46 @@ class Part1:
total += valve.rate * (30 - valve.opened_at) total += valve.rate * (30 - valve.opened_at)
return total return total
def run(self): 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 # Construct the graph with vertices & edges from the input
# Call a function to compute the distances between every pair of vertices # Call a function to compute the distances between every pair of vertices
# Create a closed set containing all the valves with non-zero rates # Create a closed set containing all the valves with non-zero rates
@ -127,7 +166,7 @@ class Part1:
if remaining <= 0: if remaining <= 0:
print("ran out of time") print("ran out of time")
break break
self.do_tick(31-remaining) self.do_tick(30-remaining)
# CALCULATE PRIORITIES # CALCULATE PRIORITIES
pris = priority(remaining) pris = priority(remaining)

View File

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

View File

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