Skip to content

Commit

Permalink
[2024/8] p1 & p2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 8, 2024
1 parent 8d04560 commit 521f316
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
Binary file modified .aoc_tiles/tiles/2024/08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions 2024/8/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
53 changes: 53 additions & 0 deletions 2024/8/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from GhostyUtils import aoc
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2
from itertools import combinations
import math


def antinodes(a: tuple, b: tuple, grid: Grid, resonates: bool = False) -> list[tuple]:
a, b = Vec2(a), Vec2(b)
v = b - a
# print(f"a: {a} b: {b} v: {v} | {a - v} {b + v}")
nodes = []
if not resonates:
aa = (a - v).as_tuple()
ab = (b + v).as_tuple()
nodes += [aa] if grid.in_bounds(aa) else []
nodes += [ab] if grid.in_bounds(ab) else []
else:
nodes += [a.as_tuple(), b.as_tuple()]
pos = a - v
while grid.in_bounds(pos):
nodes += [pos.as_tuple()]
pos -= v
pos = b + v
while grid.in_bounds(pos):
nodes += [pos.as_tuple()]
pos += v
return nodes


def main():
grid = Grid(aoc.read_lines())
antennas = {}

for element, pos in grid.by_cell():
if element != '.':
antennas.setdefault(element, []).append(pos)

antinode_positions = set()
for antenna, positions in antennas.items():
for combo in combinations(positions, 2):
antinode_positions.update(antinodes(*combo, grid))

print(f"p1: {len(antinode_positions)}")

for antenna, positions in antennas.items():
for combo in combinations(positions, 2):
antinode_positions.update(antinodes(*combo, grid, resonates=True))
print(f"p2: {len(antinode_positions)}")


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ My solutions to the yearly Advents of Code
<a href="2024/7/script.py">
<img src=".aoc_tiles/tiles/2024/07.png" width="161px">
</a>
<a href="None">
<a href="2024/8/script.py">
<img src=".aoc_tiles/tiles/2024/08.png" width="161px">
</a>
<h1 align="center">
Expand Down

0 comments on commit 521f316

Please sign in to comment.