solve day 8 part 2
This commit is contained in:
parent
c927511ee5
commit
dc8ffdfe2e
2 changed files with 34 additions and 49 deletions
|
|
@ -22,69 +22,64 @@ def get_antenna_groups(lines: list) -> dict:
|
|||
return antenna_groups
|
||||
|
||||
|
||||
def get_anti_nodes(pair: tuple, max_row: int, max_col: int) -> set:
|
||||
def get_anti_nodes(pair: tuple, max_row: int, max_col: int, resonant: bool) -> set:
|
||||
anti_nodes = set()
|
||||
a, b = pair
|
||||
a_row, a_col = a
|
||||
b_row, b_col = b
|
||||
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))
|
||||
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
|
||||
return anti_nodes
|
||||
|
||||
|
||||
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:
|
||||
def get_all_anti_nodes(antenna_groups: dict, max_row: int, max_col: int, resonant: bool) -> 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)
|
||||
new_anti_nodes = get_anti_nodes(pair, max_row, max_col, resonant)
|
||||
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)
|
||||
anti_nodes = get_all_anti_nodes(antenna_groups, max_row, max_col, False)
|
||||
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__':
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
..........
|
||||
..........
|
||||
..........
|
||||
....a.....
|
||||
..........
|
||||
.....a....
|
||||
..........
|
||||
..........
|
||||
..........
|
||||
..........
|
||||
Loading…
Add table
Add a link
Reference in a new issue