Solve day 16 and I would have made the leaderboard?

This commit is contained in:
2023-12-10 12:52:31 -05:00
parent 8ba8eaa55f
commit 4e8bd8a60c
2 changed files with 77 additions and 15 deletions

View File

@@ -11,8 +11,9 @@
- Day 9: 58:00 and 7:32 for leaderboard; I need code for 2D stuff
- Day 10: 25:20 and 12:17 for leaderboard; okay, okay
- Day 11: 45:00 and 18:00 for leaderboard; arg, it was doable man
- Day 12: 39:45 and 9:46 for leaderboard; the people are ready for there searches :D
- Day 12: 39:45 and 9:46 for leaderboard; the people are ready for their searches :D
- Day 13: 44:00 and 12:56 for leaderboard; these people are just good, seriously
- Day 14: 35:00 and 14:54; I just have to get that much quicker... 2D code!
- Day 15: 150:00 and 27:00; I didn't use Manhatten dist initially, lot's of debugging
- Day 16:
- Day 16: 52:00 and 64:00; ARE YOU SAYING I WOULD HAVE MADE THE LEADERBOARD?!?!?!?!?!?!?!
- Day 17:

87
d16.py
View File

@@ -1,36 +1,97 @@
import lib
EXAMPLE = """
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
"""
def solve(lines: list[str]):
res = 0
nodes = {}
for (i, line) in enumerate(lines):
print(i, line)
# digits = lib.str_to_int_list(line)
# digit = lib.str_to_single_int(line)
return res
source = line.split(" ")[1]
to_valves = list(map(lambda v: v.replace(",", ""), line.split(" ")[9:]))
flow_rate = lib.str_to_single_int(line)
nodes[source] = (flow_rate, to_valves)
options = [(0, 0, "AA", ())]
visited = set()
for _ in range(30):
new_options = []
for (total, flow, current, valves) in options:
valve_flow_rate = nodes[current][0]
if not current in valves and valve_flow_rate > 0:
o = (total + flow, flow + valve_flow_rate, current, valves + (current, ))
new_options.append(o)
for new_valve in nodes[current][1]:
o = (total + flow, flow, new_valve, valves)
new_options.append(o)
options = sorted(list(set(new_options)))[-100:]
return options[-1][0]
def solve2(lines: list[str]):
res = 0
nodes = {}
for (i, line) in enumerate(lines):
print(i, line)
return res
source = line.split(" ")[1]
to_valves = list(map(lambda v: v.replace(",", ""), line.split(" ")[9:]))
flow_rate = lib.str_to_single_int(line)
nodes[source] = (flow_rate, to_valves)
options = [(0, 0, ("AA", "AA"), ())]
for _ in range(26):
new_options = []
for (total, flow, (a, b), valves) in options:
flow_a = nodes[a][0]
flow_b = nodes[b][0]
# a turns on b moves
if not a in valves and flow_a > 0:
for new_valve_b in nodes[b][1]:
o = (total + flow, flow + flow_a, (a, new_valve_b), valves + (a, ))
new_options.append(o)
# a moves and b turns on
if not b in valves and flow_b > 0:
for new_valve_a in nodes[a][1]:
o = (total + flow, flow + flow_b, (new_valve_a, b), valves + (b, ))
new_options.append(o)
# both turn on
if a != b and flow_a > 0 and flow_b > 0 and not a in valves and not b in valves:
o = (total + flow, flow + flow_a + flow_b, (a, b), valves + (a, b, ))
new_options.append(o)
# both move
for new_valve_a in nodes[a][1]:
for new_valve_b in nodes[b][1]:
o = (total + flow, flow, (new_valve_a, new_valve_b), valves)
new_options.append(o)
options = sorted(list(set(new_options)))[-1000:]
# 52:00
return options[-1][0]
def main():
lines = lib.str_to_lines_no_empty(EXAMPLE)
print("Example 1:", solve(lines))
return
lines = lib.str_to_lines_no_empty(open("ix.txt").read())
lines = lib.str_to_lines_no_empty(open("i16.txt").read())
print("Solution 1:", solve(lines))
return
lines = lib.str_to_lines_no_empty(EXAMPLE)
print("Example 2:", solve2(lines))
return
lines = lib.str_to_lines_no_empty(open("ix.txt").read())
lines = lib.str_to_lines_no_empty(open("i16.txt").read())
print("Solution 2:", solve2(lines))
if __name__ == "__main__":