def part_two(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = read_input_as_string(abs_file_path) ocean_floor = {} hydrothermal_vents = [HydrothermalVent(serialized_vent) for serialized_vent in input_data] for vent in hydrothermal_vents: if vent.start_position.x == vent.end_position.x: min_y = min(vent.start_position.y, vent.end_position.y) for i in range(abs(vent.start_position.y - vent.end_position.y) + 1): key = (vent.start_position.x, min_y + i) ocean_floor[key] = ocean_floor.get(key, 0) + 1 elif vent.start_position.y == vent.end_position.y: min_x = min(vent.start_position.x, vent.end_position.x) for i in range(abs(vent.start_position.x - vent.end_position.x) + 1): key = (min_x + i, vent.start_position.y) ocean_floor[key] = ocean_floor.get(key, 0) + 1 else: step_x = -1 if vent.start_position.x > vent.end_position.x else 1 step_y = -1 if vent.start_position.y > vent.end_position.y else 1 for i in range(abs(vent.start_position.x - vent.end_position.x) + 1): key = (vent.start_position.x +(i * step_x), vent.start_position.y + (i * step_y)) ocean_floor[key] = ocean_floor.get(key, 0) + 1 overlapping_points_count = len(list(filter(lambda x: x > 1, ocean_floor.values()))) print(f"Overlapping points: {overlapping_points_count}")
def part_one_and_two(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = read_input_as_string(abs_file_path) fish_initial_ages = [ int(age) for line in input_data for age in line.split(",") ] fish_age_count = {k: 0 for k in range(0, 8 + 1)} for age in fish_initial_ages: fish_age_count[age] += 1 print(fish_age_count) for i in range(SIMULATION_DAYS): tmp_fish_age_count = {k: 0 for k in range(0, 8 + 1)} for j in range(0, 8 + 1): if j == 0: tmp_fish_age_count[6] = tmp_fish_age_count.get( 6, 0) + fish_age_count[j] tmp_fish_age_count[8] = tmp_fish_age_count.get( 8, 0) + fish_age_count[j] else: tmp_fish_age_count[j - 1] = tmp_fish_age_count.get( j - 1, 0) + fish_age_count[j] fish_age_count = tmp_fish_age_count print(sum(fish_age_count.values())) total = sum(fish_age_count.values()) print(f"Total fish: {total}")
def part_two(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") question_input = read_input_as_string(abs_file_path) scrubber_rating = get_scrubber_rating(question_input) oxygen_rating = get_oxygen_rating(question_input) print(f"Life support rating: {oxygen_rating * scrubber_rating}")
def part_one(): abs_file_path = os.path.join(os.path.dirname(__file__), "input_sample") input_data = read_input_as_string(abs_file_path) base_string = input_data[0] insertion_rules = [] # skip empty line between base string and instructions for i in range(2, len(input_data)): insertion_rule_split = input_data[i].split(" -> ") insertion_rules.append( InsertionRule( (insertion_rule_split[0][0], insertion_rule_split[0][1]), insertion_rule_split[1])) pair_wise_count = {} for i in range(1, len(base_string)): pair = (base_string[i - 1], base_string[i]) pair_wise_count[pair] = pair_wise_count.get(pair, 0) + 1 for i in range(STEPS): tmp_pair_wise_count = {} for rule in insertion_rules: tmp_pair_wise_count[rule.startPair] = tmp_pair_wise_count.get( rule.startPair, 0) + pair_wise_count.get(rule.pair, 0) tmp_pair_wise_count[rule.endPair] = tmp_pair_wise_count.get( rule.endPair, 0) + pair_wise_count.get(rule.pair, 0) # tmp_pair_wise_count[rule.pair] = 0 for key in pair_wise_count.keys(): if key not in tmp_pair_wise_count.keys(): tmp_pair_wise_count[key] = pair_wise_count[key] pair_wise_count = tmp_pair_wise_count print(tmp_pair_wise_count) print(pair_wise_count) element_count = {} for pair in pair_wise_count.keys(): element_count[pair[0]] = element_count.get(pair[0], 0) + pair_wise_count[pair] element_count[pair[1]] = element_count.get(pair[1], 0) + pair_wise_count[pair] # counts = list(element_count.values()) # print(element_count) # print(element_count.values()) min_value = min(element_count.values()) max_value = max(element_count.values()) total = max_value - min_value print(f"Total: {total}")
def part_two(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = read_input_as_string(abs_file_path) octopus_grid = [[int(octopus_energy) for octopus_energy in line] for line in input_data] for i in range(STEPS): inner_flashes = 0 is_step_complete = False for y in range(0, len(octopus_grid)): print(''.join([str(octo) for octo in octopus_grid[y]])) print("------------------------------------") for y in range(0, len(octopus_grid)): for x in range(0, len(octopus_grid[y])): if octopus_grid[y][x] != -1: octopus_grid[y][x] += 1 while not is_step_complete: has_flashed = False for y in range(0, len(octopus_grid)): for x in range(0, len(octopus_grid[y])): if octopus_grid[y][x] > 9: octopus_grid[y][x] = -1 inner_flashes += 1 has_flashed = True # up safe_update(x, y + 1, octopus_grid) safe_update(x - 1, y + 1, octopus_grid) safe_update(x + 1, y + 1, octopus_grid) # down safe_update(x, y - 1, octopus_grid) safe_update(x - 1, y - 1, octopus_grid) safe_update(x + 1, y - 1, octopus_grid) # left / right safe_update(x + 1, y, octopus_grid) safe_update(x - 1, y, octopus_grid) is_step_complete = not has_flashed # reset -1 to 0's for y in range(0, len(octopus_grid)): for x in range(0, len(octopus_grid[y])): if octopus_grid[y][x] == -1: octopus_grid[y][x] = 0 if inner_flashes == (len(octopus_grid) * len(octopus_grid[0])): break print(f"Step: {i + 1}")
def part_one(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = [int(pos) for pos in read_input_as_string(abs_file_path)[0].split(",")] min_crab_pos = min(input_data) max_crab_pos = max(input_data) cost_per_pos = {} for i in range(min_crab_pos, max_crab_pos + 1): fuel_usage = 0 for crab in input_data: fuel_usage += abs(crab - i) cost_per_pos[i] = fuel_usage min_fuel = 99999999 for fuel in cost_per_pos.values(): if fuel < min_fuel: min_fuel = fuel print(f"Fuel: {min_fuel}")
def part_one(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = read_input_as_string(abs_file_path) height_map = [[int(height) for height in line] for line in input_data] low_points = [] for y in range(0, len(height_map)): for x in range(0, len(height_map[y])): current_val = height_map[y][x] if (is_lower_location(x, y, x, y - 1, height_map) and is_lower_location(x, y, x, y + 1, height_map) and is_lower_location(x, y, x + 1, y, height_map) and is_lower_location(x, y, x - 1, y, height_map)): low_points.append(current_val) else: pass risk_level_total = sum(low_points) + len(low_points) print(f"Total risk level: {risk_level_total}")
def part_one(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") question_input = read_input_as_string(abs_file_path) position = 0 depth = 0 for i in question_input: command_values = i.split(" ") command = command_values[0].strip() value = int(command_values[1].strip()) if command == FORWARD: position += value if command == DOWN: depth += value if command == UP: depth -= value print("Depth: " + str(depth)) print("Position: " + str(position)) print("Total: " + str(depth * position))
def part_one(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") question_input = read_input_as_string(abs_file_path) bit_positions_on = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] bit_positions_off = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for i in range(0, len(question_input)): line = question_input[i] for j in range(0, len(question_input[i])): char = line[j] if char == "1": bit_positions_on[j] += 1 else: bit_positions_off[j] += 1 gamma_str = "".join(["0" if tup[0] > tup[1] else "1" for tup in zip(bit_positions_on, bit_positions_off)]) alpha_str = "".join(["1" if tup[0] > tup[1] else "0" for tup in zip(bit_positions_on, bit_positions_off)]) print(f"gamma: {gamma_str}") print(f"alpha: {alpha_str}") print(f"Total: {int(gamma_str, 2) * int(alpha_str, 2)}")
def part_one(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = read_input_as_string(abs_file_path) chunk_pairs = { "(": ")", "[": "]", "{": "}", "<": ">", } chunk_points = { ")": 3, "]": 57, "}": 1197, ">": 25137, } illegal_characters = [] for line in input_data: open_chunks = [] for i in range(0, len(line)): current_char = line[i] if current_char in chunk_pairs.keys(): open_chunks.append(current_char) else: current_open = open_chunks.pop() if chunk_pairs[current_open] != current_char: illegal_characters.append(current_char) break total = 0 for char in illegal_characters: total += chunk_points[char] print(f"Total: {total}")
def part_two(): abs_file_path = os.path.join(os.path.dirname(__file__), "input") input_data = read_input_as_string(abs_file_path) chunk_pairs = { "(": ")", "[": "]", "{": "}", "<": ">", } chunk_points = { ")": 1, "]": 2, "}": 3, ">": 4, } valid_lines = [] for line in input_data: open_chunks = [] is_valid_line = True for i in range(0, len(line)): current_char = line[i] if current_char in chunk_pairs.keys(): open_chunks.append(current_char) else: current_open = open_chunks.pop() if chunk_pairs[current_open] != current_char: is_valid_line = False break if is_valid_line: valid_lines.append(line) corrected_lines = [] for line in valid_lines: new_line = "" open_chunks = [] for i in range(0, len(line)): current_char = line[i] if current_char in chunk_pairs.keys(): open_chunks.append(current_char) else: open_chunks.pop() for char in reversed(open_chunks): new_line = new_line + chunk_pairs[char] corrected_lines.append(new_line) totals = [] for line in corrected_lines: total = 0 for char in line: total = (total * 5) + chunk_points[char] totals.append(total) print(totals) sorted_totals = list(sorted(totals)) score_index = int(len(sorted_totals) / 2) print(f"Total {sorted_totals[score_index]}")