my solutions for advent of code 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

2 years ago
import queue
q = queue.Queue()
valves = {}
valvesConsider = {}
destinationQue = queue.Queue()
shortest = None
distanceMap = {}
class Valve():
def __init__(self, flowRate, tunnels, name):
self.flowRate = flowRate
self.tunnels = tunnels
self.name = name
def getFlowrate(self):
return self.flowRate
def moves(self, destination, length):
global shortest
if shortest is not None and length >= shortest:
return
if self.name == destination:
if shortest is None or length <= shortest:
shortest = length
return
_length = length + 1
for val in self.tunnels:
destinationQue.put((val, destination, _length))
def step(position, currentTime, pressure, _valvesCons):
global curentbest
_pos = position
best = None
for destination in _valvesCons:
_openvalves = _valvesCons.copy()
dist = distanceMap[(_pos, destination)]
value = (30 - currentTime - dist - 1) * _openvalves[destination]
best = (destination, value , dist)
_time = currentTime + best[2] + 1
if _time >= 30:
continue
_pressure = pressure + best[1]
_position = best[0]
_openvalves.pop(destination)
q.put((_position, _time, _pressure, _openvalves))
if pressure > curentbest:
curentbest = pressure
def distance(position, destination):
global destinationQue
global shortest
shortest = None
destinationQue.put((position, destination, 0))
while not destinationQue.empty():
_position, _destination, length = destinationQue.get()
valves[_position].moves(_destination, length)
return shortest
openvalves = []
with open('input16.txt','r') as f:
inp = f.read().splitlines(keepends=False)
for line in inp:
tempA, tempB = line.split(' has flow rate=')
_valve = tempA[-2:]
tempB = tempB.replace('; tunnel leads to valve ','; tunnels lead to valves ')
flowRate, tempA = tempB.split('; tunnels lead to valves ')
flowRate = int(flowRate)
tunnels = tempA.split(', ')
if flowRate == 0:
openvalves.append(_valve)
else:
valvesConsider[_valve] = flowRate
newValve = Valve( flowRate, tunnels, _valve)
valves[_valve] = newValve
n = 0
for start in valvesConsider:
n += 1
distanceMap[('AA', start)] = distance('AA', start)
print(n)
for end in valvesConsider:
if (start, end) in distanceMap:
continue
n += 1
_dist = distance(start, end)
distanceMap[(start, end)] = _dist
distanceMap[(end, start)] = _dist
print(n)
for i in distanceMap:
print(i, distanceMap[i])
position = 'AA'
currentTime = 0
pressure = 0
curentbest = 0
q.put((position, currentTime, pressure, valvesConsider))
while not q.empty():
print(q.qsize())
position, currentTime, pressure, _valvesConsider = q.get()
step(position, currentTime, pressure, _valvesConsider)
print(curentbest)