Last active
December 8, 2024 07:10
-
-
Save queercat/b2ca984ca9df199054732400e30ce759 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| challenge = """ | |
| ......#....# | |
| ...#....0... | |
| ....#0....#. | |
| ..#....0.... | |
| ....0....#.. | |
| .#....A..... | |
| ...#........ | |
| #......#.... | |
| ........A... | |
| .........A.. | |
| ..........#. | |
| ..........#. | |
| """ | |
| challenge = challenge.split("\n") | |
| challenge = [[x for x in c] for c in challenge if len(c) > 0] | |
| nodes = {} | |
| def is_aligned(left, right): | |
| if abs(left[0] - right[0]) == 1 or abs(left[1] - right[1]) == 1: | |
| if abs(left[0] - right[0]) == 1 and abs(left[1] - right[1]) == 1: | |
| return ("x", "y") | |
| elif abs(left[0] - right[0]) == 1: | |
| return ("x") | |
| elif abs(left[1] - right[1]) == 1: | |
| return ("y") | |
| return () | |
| aligned = set() | |
| for idx in range(len(challenge)): | |
| for idy in range(len(challenge[idx])): | |
| value = challenge[idx][idy] | |
| if value != "." and value != "#": | |
| if value not in nodes: | |
| nodes[value] = [] | |
| nodes[value].append((idx, idy)) | |
| for node in nodes: | |
| nodes[node] = sorted(nodes[node], key = lambda v: v[0]) | |
| for idx in range(len(nodes[node]) - 1): | |
| left, right = nodes[node][idx], nodes[node][idx + 1] | |
| result = is_aligned(left, right) | |
| aligned.add((left, right, result)) | |
| nodes[node] = sorted(nodes[node], key = lambda v: v[1]) | |
| for idx in range(len(nodes[node]) - 1): | |
| left, right = nodes[node][idx], nodes[node][idx + 1] | |
| result = is_aligned(left, right) | |
| aligned.add((left, right, result)) | |
| def distance(left, right): | |
| a, b = left | |
| c, d = right | |
| return ((((a - c) ** 2) + ((b - d) ** 2))) ** .5 | |
| antinodes = set() | |
| for pair in aligned: | |
| # find a position on the map where it is x distance from left and 2x distance from right | |
| left, right, axis = pair | |
| candidates = [] | |
| if 'x' in axis: | |
| min_x = min(left, right, key = lambda v: v[0])[0] | |
| max_x = max(left, right, key = lambda v: v[0])[0] | |
| for idy in range(len(challenge[0])): | |
| candidates.append((min_x - 1, idy)) | |
| candidates.append((max_x + 1, idy)) | |
| if 'y' in axis: | |
| min_y = min(left, right, key = lambda v: v[1])[1] | |
| max_y = max(left, right, key = lambda v: v[1])[1] | |
| for idx in range(len(challenge)): | |
| candidates.append((idx, min_y - 1)) | |
| candidates.append((idx, max_y + 1)) | |
| for candidate in candidates: | |
| dl = distance(candidate, left) | |
| dr = distance(candidate, right) | |
| if candidate == (2,4): | |
| print(candidate, left, right, challenge[left[0]][left[1]], challenge[right[0]][right[1]]) | |
| if candidate[0] < 0 or candidate[0] >= len(challenge) or candidate[1] < 0 or candidate[1] >= len(challenge[candidate[0]]): | |
| continue | |
| if (dl * 2 == dr) or (dr * 2 == dl): | |
| antinodes.add((candidate[0], candidate[1])) | |
| for x in range(len(challenge)): | |
| for y in range(len(challenge[x])): | |
| if challenge[x][y] == "#": | |
| challenge[x][y] = "." | |
| for x, y in antinodes: | |
| challenge[x][y] = f"#" | |
| print(len(antinodes)) | |
| challenge = ["".join(c) for c in challenge] | |
| for idx, line in enumerate(challenge): | |
| print(" ".join(line), idx) | |
| for idx in range(len(challenge[0])): | |
| print(idx, end=" ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment