def tests(): for i, t in enumerate(test_list): rxn_dict = {} used_in_dict = defaultdict(list) for line in t.split("\n"): parse_rxn(line, rxn_dict, used_in_dict) complexity_list = topo_sort(used_in_dict) a = find_ore_cost(1, "FUEL", copy(complexity_list), rxn_dict) assert a == answer_list[i] RXN_DICT = {} USED_IN_DICT = defaultdict(list) line_count = 0 fn = get_input_file_name("d14.txt") with open(fn, "r") as f: for line in f.readlines(): line_count += 1 parse_rxn(line.strip(), RXN_DICT, USED_IN_DICT) assert len(RXN_DICT) == line_count complexity_list = topo_sort(USED_IN_DICT) assert len(complexity_list) == len(RXN_DICT) + 1 # ORE not in RXN_DICT print("Part 1:\n", (per := find_ore_cost(1, "FUEL", copy(complexity_list), RXN_DICT))) # Iterate until we find right answer guess = floor(10**12 / per) budget = 10**12 while ((curr := find_ore_cost(guess, "FUEL", copy(complexity_list), RXN_DICT))
t += 1 new_air = set() has_air |= just_got_air while just_got_air: r = just_got_air.pop() for neighb in connections[r]: if neighb not in has_air: new_air.add(neighb) no_air.discard(neighb) just_got_air = new_air if t > len(connections): return False return t file_name = get_input_file_name("d15.txt") bot = RepairBot(parse_input(file_name), []) connections = defaultdict(list) directs = {1: Point(0, 1), 2: Point(0, -1), 3: Point(-1, 0), 4: Point(1, 0)} r_directs = {Point(0, 1): 1, Point(0, -1): 2, Point(-1, 0): 3, Point(1, 0): 4} oxy_pos = list() current_pos = Point(0, 0) visited = set() bot.run() DFS(Point(0, 0)) for p1, l in connections.items(): for p2 in l: assert p2 - p1 in r_directs print("Part 1:\n", a_star(connections, oxy_pos[0])) print("Part 2:\n", oxy(connections, oxy_pos[0]))