Browse Source

day 16

main
Luna Lailatova 1 year ago
parent
commit
ba00961766
  1. 114
      day16-1.py
  2. 137
      day16-2.py
  3. 118
      day16-2take2.py
  4. 24
      in.txt
  5. 60
      input16.txt

114
day16-1.py

@ -0,0 +1,114 @@ @@ -0,0 +1,114 @@
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)

137
day16-2.py

@ -0,0 +1,137 @@ @@ -0,0 +1,137 @@
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(position1, position2, busy1, busy2, currentTime, pressure, _valvesCons):
global curentbest
done = False
if pressure > curentbest:
print(1)
curentbest = pressure
if len(_valvesCons) == 0:
return
if busy1 == 0:
for destination in _valvesCons:
_CopyCons = _valvesCons.copy()
_dist = distanceMap[(position1, destination)]
if (currentTime + _dist + 1) >= 26:
continue
_busy1 =_dist + 1
_pressure = pressure + ((26 - currentTime - _busy1)* _CopyCons[destination])
_CopyCons.pop(destination)
done = True
q.put((destination, position2, _busy1, busy2, currentTime, _pressure, _CopyCons))
if done:
return
if busy2 == 0:
for destination in _valvesCons:
_CopyCons = _valvesCons.copy()
_dist = distanceMap[(position2, destination)]
if (currentTime + _dist + 1) >= 26:
continue
_busy2 =_dist + 1
_pressure = pressure + ((26 - currentTime - _busy2)* _CopyCons[destination])
_CopyCons.pop(destination)
q.put((position1, destination, busy1, _busy2, currentTime, _pressure, _CopyCons))
return
_currentTime = currentTime + 1
_busy1 = busy1 - 1
_busy2 = busy2 - 1
_CopyCons = _valvesCons.copy()
if _currentTime >= 26:
return
q.put((position1, position2, _busy1, _busy2, _currentTime, pressure, _CopyCons))
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, position, 0, 0, currentTime, pressure, valvesConsider))
while not q.empty():
print(q.qsize())
position1, position2, busy1, busy2, currentTime, pressure, _valvesConsider = q.get()
step(position1, position2, busy1, busy2, currentTime, pressure, _valvesConsider)
print(curentbest)

118
day16-2take2.py

@ -0,0 +1,118 @@ @@ -0,0 +1,118 @@
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, elephant):
global curentbest
_pos = position
best = None
for destination in _valvesCons:
_elephant = elephant
_openvalves = _valvesCons.copy()
dist = distanceMap[(_pos, destination)]
value = (26 - currentTime - dist - 1) * _openvalves[destination]
best = (destination, value , dist)
_time = currentTime + best[2] + 1
if _time >= 26:
if _elephant:
continue
q.put(('AA', 0, pressure, _openvalves, True))
continue
_pressure = pressure + best[1]
_position = best[0]
_openvalves.pop(destination)
q.put((_position, _time, _pressure, _openvalves, elephant))
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('in.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, False))
while not q.empty():
print(q.qsize())
position, currentTime, pressure, _valvesConsider, elephant = q.get()
step(position, currentTime, pressure, _valvesConsider, elephant)
print(curentbest)

24
in.txt

@ -1,14 +1,10 @@ @@ -1,14 +1,10 @@
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II

60
input16.txt

@ -0,0 +1,60 @@ @@ -0,0 +1,60 @@
Valve NQ has flow rate=0; tunnels lead to valves SU, XD
Valve AB has flow rate=0; tunnels lead to valves XD, TE
Valve IA has flow rate=0; tunnels lead to valves CS, WF
Valve WD has flow rate=0; tunnels lead to valves DW, II
Valve XD has flow rate=10; tunnels lead to valves AB, NQ, VT, SC, MU
Valve SL has flow rate=0; tunnels lead to valves RP, DS
Valve FQ has flow rate=15; tunnels lead to valves EI, YC
Valve KF has flow rate=0; tunnels lead to valves FL, QP
Valve QP has flow rate=0; tunnels lead to valves KF, RP
Valve DS has flow rate=0; tunnels lead to valves SL, AA
Valve IK has flow rate=0; tunnels lead to valves XC, AA
Valve HQ has flow rate=0; tunnels lead to valves VM, WV
Valve WR has flow rate=0; tunnels lead to valves WV, HF
Valve HH has flow rate=20; tunnels lead to valves PI, CF, CN, NF, AR
Valve DW has flow rate=19; tunnels lead to valves KD, WD, HS
Valve RP has flow rate=14; tunnels lead to valves SL, QP, BH, LI, WP
Valve EC has flow rate=0; tunnels lead to valves NF, XC
Valve AA has flow rate=0; tunnels lead to valves NH, ES, UC, IK, DS
Valve VM has flow rate=18; tunnel leads to valve HQ
Valve NF has flow rate=0; tunnels lead to valves HH, EC
Valve PS has flow rate=0; tunnels lead to valves AR, SU
Valve IL has flow rate=0; tunnels lead to valves XC, KZ
Valve WP has flow rate=0; tunnels lead to valves CS, RP
Valve WF has flow rate=0; tunnels lead to valves FL, IA
Valve XW has flow rate=0; tunnels lead to valves OL, NL
Valve EH has flow rate=0; tunnels lead to valves UK, YR
Valve UC has flow rate=0; tunnels lead to valves AA, FL
Valve CS has flow rate=3; tunnels lead to valves IA, CN, LD, RJ, WP
Valve AR has flow rate=0; tunnels lead to valves PS, HH
Valve CF has flow rate=0; tunnels lead to valves HH, FL
Valve NH has flow rate=0; tunnels lead to valves AA, LD
Valve RJ has flow rate=0; tunnels lead to valves DJ, CS
Valve XC has flow rate=17; tunnels lead to valves IL, EC, YR, IK, DJ
Valve TE has flow rate=24; tunnels lead to valves AB, YA
Valve CN has flow rate=0; tunnels lead to valves HH, CS
Valve KD has flow rate=0; tunnels lead to valves DW, UK
Valve SC has flow rate=0; tunnels lead to valves EI, XD
Valve MU has flow rate=0; tunnels lead to valves XD, YP
Valve SU has flow rate=22; tunnels lead to valves PS, LI, II, NQ
Valve FL has flow rate=8; tunnels lead to valves KF, WF, CF, UC, HS
Valve OL has flow rate=4; tunnels lead to valves KZ, HF, XW
Valve EI has flow rate=0; tunnels lead to valves FQ, SC
Valve NL has flow rate=0; tunnels lead to valves XW, UK
Valve YP has flow rate=21; tunnels lead to valves YA, MU, YC
Valve BH has flow rate=0; tunnels lead to valves VT, RP
Valve II has flow rate=0; tunnels lead to valves SU, WD
Valve YA has flow rate=0; tunnels lead to valves TE, YP
Valve HS has flow rate=0; tunnels lead to valves FL, DW
Valve DJ has flow rate=0; tunnels lead to valves RJ, XC
Valve KZ has flow rate=0; tunnels lead to valves OL, IL
Valve YR has flow rate=0; tunnels lead to valves EH, XC
Valve UK has flow rate=7; tunnels lead to valves KD, NL, EH
Valve YC has flow rate=0; tunnels lead to valves FQ, YP
Valve ES has flow rate=0; tunnels lead to valves PI, AA
Valve LI has flow rate=0; tunnels lead to valves SU, RP
Valve LD has flow rate=0; tunnels lead to valves NH, CS
Valve VT has flow rate=0; tunnels lead to valves BH, XD
Valve PI has flow rate=0; tunnels lead to valves ES, HH
Valve WV has flow rate=11; tunnels lead to valves WR, HQ
Valve HF has flow rate=0; tunnels lead to valves OL, WR
Loading…
Cancel
Save