def test_monitoring_station(input_file_num, num_detectable, nth_destroyed): _asteroid_map = create_asteroid_map( read(f"monitoring_station{input_file_num}.txt")) _best_asteroid, _detectable = find_asteroid_with_most_other_detectable_asteroids( _asteroid_map) assert len(_detectable) == num_detectable actual_nth_destroyed = find_nth_destroyed_asteroid(_best_asteroid, _asteroid_map, 200) assert actual_nth_destroyed == nth_destroyed
def _replace_in_common_paths(key, common_paths, key_to_robot): robot = key if key.isnumeric() else key_to_robot[key] for old_key in common_paths: if old_key == robot or (old_key.isalpha() and key_to_robot[old_key] == robot): break common_paths = dict(common_paths) common_paths.pop(old_key) common_paths[key] = (0, ()) return common_paths if __name__ == '__main__': world_map = read("keys2.txt") i = 0 while '@' in world_map: world_map = world_map.replace('@', str(i), 1) i += 1 world_map = list(clean_lines_iter(world_map)) _robots = list(find_x(world_map, r"\d")) _keys = find_x(world_map, "[a-z]") _doors = find_x(world_map, "[A-Z]") _passage = find_x(world_map, r"[a-zA-Z\d.]") _passage = parse_passage(_passage) all_shortest_paths = {} for x in _robots + list(_keys):
self.cost += amount return produced_by_one_reaction, reaction = self.reactions[material] n_to_produce = ceil( (amount - self.leftover[material]) / produced_by_one_reaction) if n_to_produce > 0: for child_material, child_amount in reaction: self.produce(child_material, n_to_produce * child_amount) self.leftover[ material] += n_to_produce * produced_by_one_reaction - amount if __name__ == '__main__': reactions = parse_reactions(read("reactions1.txt")) factory = MaterialFactory(reactions) factory.produce("FUEL", 1) print(factory.cost) n = 1 budget = 1000000000000 while factory.cost < budget: n += 1 factory.produce("FUEL", 1) print(f"\r{factory.cost / budget}", end="") print() if factory.cost == budget:
def test_moons(input_file_num, n_steps, expected_energy, expected_cycle_time): moons = parse_moons_str(read(f"moons{input_file_num}.txt")) assert find_total_energy_after_n_steps(moons, n_steps) == expected_energy assert find_cycle_time(moons) == expected_cycle_time
key=lambda ast: len(detectable_per_asteroid[ast])) return best_asteroid, detectable_per_asteroid[best_asteroid] def find_nth_destroyed_asteroid(monitoring_asteroid, asteroid_map, n): asteroid_map = deepcopy(asteroid_map) destroyed = [] detectable = find_all_detectable_asteroids(monitoring_asteroid, get_asteroids(asteroid_map)) while detectable: for other_asteroid in detectable.values(): i, j = other_asteroid asteroid_map[i][j] = False destroyed.append(other_asteroid) detectable = find_all_detectable_asteroids(monitoring_asteroid, get_asteroids(asteroid_map)) return destroyed[n - 1] if __name__ == '__main__': _asteroid_map = create_asteroid_map(read("monitoring_station2.txt")) _best_asteroid, _detectable = find_asteroid_with_most_other_detectable_asteroids( _asteroid_map) print(_best_asteroid, len(_detectable)) nth_destroyed = find_nth_destroyed_asteroid(_best_asteroid, _asteroid_map, 200) print(100 * nth_destroyed[1] + nth_destroyed[0])
def find_cycle_time(moons): moons = deepcopy(moons) original_moons = deepcopy(moons) t = 0 phases = [0] * 3 while not all(phases): step(moons) t += 1 for i in range(3): if phases[i]: continue if is_repeat(moons, original_moons, i): phases[i] = t return lcm(phases) def parse_moons_str(moons_str): moons = [] for moon_str in filter(lambda x: x, map(lambda x: x.strip(), moons_str.splitlines())): moons.append(create_moon(moon_str)) return moons if __name__ == '__main__': moons_str = read("moons2.txt") moons = parse_moons_str(moons_str) print(find_total_energy_after_n_steps(moons, 1000)) print(find_cycle_time(moons))