diff --git a/day_08/program.py b/day_08/program.py index 62edd60..2daf652 100755 --- a/day_08/program.py +++ b/day_08/program.py @@ -22,64 +22,69 @@ def get_antenna_groups(lines: list) -> dict: return antenna_groups -def get_anti_nodes(pair: tuple, max_row: int, max_col: int, resonant: bool) -> set: +def get_anti_nodes(pair: tuple, max_row: int, max_col: int) -> set: anti_nodes = set() a, b = pair a_row, a_col = a b_row, b_col = b - if resonant: - anti_nodes.add((a_row, a_col)) - anti_nodes.add((b_row, b_col)) - delta_row = b_row - a_row - delta_col = b_col - a_col - loop = True - a_invalid = False - b_invalid = False - while loop: - anti_a_row = a_row - delta_row - anti_a_col = a_col - delta_col - anti_b_row = b_row + delta_row - anti_b_col = b_col + delta_col - if 0 <= anti_a_row <= max_row and 0 <= anti_a_col <= max_col: - anti_nodes.add((anti_a_row, anti_a_col)) - else: - a_invalid = True - if 0 <= anti_b_row <= max_row and 0 <= anti_b_col <= max_col: - anti_nodes.add((anti_b_row, anti_b_col)) - else: - b_invalid = True - if a_invalid and b_invalid: - loop = False - if resonant: - a_row, a_col = anti_a_row, anti_a_col - b_row, b_col = anti_b_row, anti_b_col - else: - loop = False + vektor_row = b_row - a_row + vektor_col = b_col - a_col + anti_a_row = a_row - vektor_row + anti_a_col = a_col - vektor_col + if 0 <= anti_a_row <= max_row and 0 <= anti_a_col <= max_col: + anti_nodes.add((anti_a_row, anti_a_col)) + anti_b_row = b_row + vektor_row + anti_b_col = b_col + vektor_col + if 0 <= anti_b_row <= max_row and 0 <= anti_b_col <= max_col: + anti_nodes.add((anti_b_row, anti_b_col)) return anti_nodes -def get_all_anti_nodes(antenna_groups: dict, max_row: int, max_col: int, resonant: bool) -> set: +def print_field(antennas: list, new_anti_nodes: set, max_row: int, max_col: int) -> None: + field = [] + for i in range(max_row + 1): + row = [] + for j in range(max_col + 1): + row.append(".") + field.append(row) + for antenna in antennas: + row, col = antenna + field[row][col] = "A" + for anti_node in new_anti_nodes: + row, col = anti_node + if field[row][col] == "A": + field[row][col] = "@" + else: + field[row][col] = "#" + for row in field: + for char in row: + print(char, end="") + print() + print() + + +def get_all_anti_nodes(antenna_groups: dict, max_row: int, max_col: int) -> set: anti_nodes = set() for antenna_group in antenna_groups: antennas = antenna_groups[antenna_group] antenna_pairs = combinations(antennas, 2) for pair in antenna_pairs: - new_anti_nodes = get_anti_nodes(pair, max_row, max_col, resonant) + new_anti_nodes = get_anti_nodes(pair, max_row, max_col) anti_nodes.update(new_anti_nodes) + # print_field(antennas, anti_nodes, max_row, max_col) return anti_nodes def main(): # lines = get_lines("sample-input.txt") + # lines = get_lines("sample-input2.txt") lines = get_lines("input.txt") antenna_groups = get_antenna_groups(lines) max_row = len(lines) - 1 max_col = len(lines[0]) - 1 - anti_nodes = get_all_anti_nodes(antenna_groups, max_row, max_col, False) + anti_nodes = get_all_anti_nodes(antenna_groups, max_row, max_col) print(f"Part 1: There are {len(anti_nodes)} anti-nodes on the map.") - anti_nodes = get_all_anti_nodes(antenna_groups, max_row, max_col, True) - print(f"Part 2: There are {len(anti_nodes)} resonant anti-nodes on the map.") if __name__ == '__main__': diff --git a/day_08/sample-input2.txt b/day_08/sample-input2.txt new file mode 100644 index 0000000..86a4982 --- /dev/null +++ b/day_08/sample-input2.txt @@ -0,0 +1,10 @@ +.......... +.......... +.......... +....a..... +.......... +.....a.... +.......... +.......... +.......... +.......... diff --git a/day_09/program.py b/day_09/program.py deleted file mode 100755 index 3247ddb..0000000 --- a/day_09/program.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python3 - -# https://adventofcode.com/2024/day/9 - -def get_lines(filename: str) -> list: - with open(filename, "r") as file: - return [line.strip() for line in file.readlines()] - - -def main(): - lines = get_lines("sample-input.txt") - # lines = get_lines("input.txt") - - -if __name__ == '__main__': - main()