Compare commits
No commits in common. "dabc0d65bb3ff4535fecc8a6c3edba7437269c70" and "c927511ee59da4a1000c1e24e665f99cb2579da6" have entirely different histories.
dabc0d65bb
...
c927511ee5
3 changed files with 49 additions and 50 deletions
|
|
@ -22,64 +22,69 @@ def get_antenna_groups(lines: list) -> dict:
|
||||||
return antenna_groups
|
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()
|
anti_nodes = set()
|
||||||
a, b = pair
|
a, b = pair
|
||||||
a_row, a_col = a
|
a_row, a_col = a
|
||||||
b_row, b_col = b
|
b_row, b_col = b
|
||||||
if resonant:
|
vektor_row = b_row - a_row
|
||||||
anti_nodes.add((a_row, a_col))
|
vektor_col = b_col - a_col
|
||||||
anti_nodes.add((b_row, b_col))
|
anti_a_row = a_row - vektor_row
|
||||||
delta_row = b_row - a_row
|
anti_a_col = a_col - vektor_col
|
||||||
delta_col = b_col - a_col
|
if 0 <= anti_a_row <= max_row and 0 <= anti_a_col <= max_col:
|
||||||
loop = True
|
anti_nodes.add((anti_a_row, anti_a_col))
|
||||||
a_invalid = False
|
anti_b_row = b_row + vektor_row
|
||||||
b_invalid = False
|
anti_b_col = b_col + vektor_col
|
||||||
while loop:
|
if 0 <= anti_b_row <= max_row and 0 <= anti_b_col <= max_col:
|
||||||
anti_a_row = a_row - delta_row
|
anti_nodes.add((anti_b_row, anti_b_col))
|
||||||
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
|
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()
|
anti_nodes = set()
|
||||||
for antenna_group in antenna_groups:
|
for antenna_group in antenna_groups:
|
||||||
antennas = antenna_groups[antenna_group]
|
antennas = antenna_groups[antenna_group]
|
||||||
antenna_pairs = combinations(antennas, 2)
|
antenna_pairs = combinations(antennas, 2)
|
||||||
for pair in antenna_pairs:
|
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)
|
anti_nodes.update(new_anti_nodes)
|
||||||
|
# print_field(antennas, anti_nodes, max_row, max_col)
|
||||||
return anti_nodes
|
return anti_nodes
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# lines = get_lines("sample-input.txt")
|
# lines = get_lines("sample-input.txt")
|
||||||
|
# lines = get_lines("sample-input2.txt")
|
||||||
lines = get_lines("input.txt")
|
lines = get_lines("input.txt")
|
||||||
|
|
||||||
antenna_groups = get_antenna_groups(lines)
|
antenna_groups = get_antenna_groups(lines)
|
||||||
max_row = len(lines) - 1
|
max_row = len(lines) - 1
|
||||||
max_col = len(lines[0]) - 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.")
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
10
day_08/sample-input2.txt
Normal file
10
day_08/sample-input2.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
....a.....
|
||||||
|
..........
|
||||||
|
.....a....
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
|
@ -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()
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue