Unable to load image

day 16 aoc thread: the np is complete

this is travelling salesmen, eh? i'm pretty sure at least.

14
Jump in the discussion.

No email address required.

im just so fricking sick of my solution working on the test input but not the real input. it's happened like 4 days in a fricking row now. how am i supposed to debug this shit?

Jump in the discussion.

No email address required.

The old school way, like you'd do if you didn't have a test input at all!

By "doesn't work" do you mean that it gives a wrong answer? Is the answer too low (you could be using a wrong algo) or too high (you have a bug)? Have you noticed that the starting node is "AA" not the first line in input :marseyfacepalm: ?

Jump in the discussion.

No email address required.

NEVER MIND

I BEAT PART 1 HOLY SHIT

:#soyjackwow: :#soyjackwow: :#soyjackwow::#soyjackwow::#soyjackwow:

i originally had a thing that checked if the time spent was above 10 and pressure was less than 500, then it would abort (to stop it taking fucking forever). that worked on the test, but gave the wrong answer on the real input. i changed it to time > 15 instead and it worked, which was a much more reasonable reason to fail than the previous ones. pretty sure the code i wrote is extremely unholy; i barely even remember the parts i wrote at the start of the day. pretty happy tho

import parse
from typing import List
import networkx as nx

with open("d16i.txt") as f:
    input = f.read()

names: List[str] = [x[0] if x[0] != "SD\nValve ZX" else "ZX" for x in parse.findall("Valve {} has", input)]
flow_rates: List[int] = [x[0] for x in parse.findall("flow rate={:d}", input)]
tunnels = {}
for i, line in enumerate(input.split("\n")):
    tunnels[names[i]] = tuple(thing[:2] for thing in line.split(" ")[9:]) 
tlm = {name: flow_rate for name, flow_rate in zip(names, flow_rates) if flow_rate > 0 or name == "AA"}

graph1 = nx.Graph()
graph2 = nx.Graph()

for name in names:
    graph1.add_node(name)

for name, connections in tunnels.items():
    for connection in connections:
        graph1.add_edge(name, connection)

for name in tlm:
    graph2.add_node(name)

for name in tlm:
    graph2.add_weighted_edges_from([(name, valve, nx.shortest_path_length(graph1, name, valve)) for valve in tlm])

def add_pressure(valves):
    turned = set(valve for valve in valves if valves.count(valve) > 1)
    return sum(tlm[valve] for valve in turned)

all_paths = []
def create_paths(this_path=["AA"], time_spent=0, pressure=0):
    if time_spent > 15 and pressure < 500:
        return
    if time_spent == 30:
        all_paths.append(pressure)
        return
    choices = []
    for valve in tlm:
        if valve == this_path[-1]:
            if not this_path.count(valve) > 1:
                choices.append(valve)
        elif not graph2.get_edge_data(this_path[-1], valve)["weight"] + time_spent > 30 and valve not in this_path:
            choices.append(valve)
    if not choices:
        while time_spent < 30:
            time_spent += 1
            pressure += add_pressure(this_path)
        all_paths.append(pressure)
    for choice in choices:
        if choice == this_path[-1]:
            create_paths(this_path + [choice], time_spent + 1, pressure + add_pressure(this_path))
        else:
            mins = graph2.get_edge_data(this_path[-1], choice)["weight"]
            add_p = 0
            for _ in range(mins):
                add_p += add_pressure(this_path)
            create_paths(this_path + [choice], time_spent + mins, pressure + add_p)

create_paths()
print(sorted(all_paths)[-1])
Jump in the discussion.

No email address required.

All those words won't bring daddy back.

Jump in the discussion.

No email address required.

:#marseydunkon:

Jump in the discussion.

No email address required.

Congratulations! Btw, you don't have to explicitly call graph.add_node unless you want to add attributes on it or something.

Jump in the discussion.

No email address required.

@Bussy-boy frick you loser

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.