Skip to content

Commit 83b6433

Browse files
Add Day 8: Part 2
1 parent 18ff5be commit 83b6433

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
| [Day 5: Print Queue](https://adventofcode.com/2024/day/5) ||| [day5.py](aoc/day5.py) |
1212
| [Day 6: Guard Gallivant](https://adventofcode.com/2024/day/6) ||| [day6.py](aoc/day6.py) |
1313
| [Day 7: Bridge Repair](https://adventofcode.com/2024/day/7) ||| [day7.py](aoc/day7.py) |
14-
| [Day 8: Resonant Collinearity](https://adventofcode.com/2024/day/8) || | [day8.py](aoc/day8.py) |
14+
| [Day 8: Resonant Collinearity](https://adventofcode.com/2024/day/8) || | [day8.py](aoc/day8.py) |

aoc/day8.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def parse_input(lines: list[str]) -> AntennaMap:
1919
return AntennaMap(len(lines), len(lines[0]), antennamap)
2020

2121

22-
def positions(am: AntennaMap) -> set[tuple[int, int]]:
22+
def positions(am: AntennaMap, mul=1) -> set[tuple[int, int]]:
2323
anodes_pos = set()
2424

2525
def is_in_field(pos):
@@ -29,7 +29,9 @@ def is_in_field(pos):
2929
for _, positions in am.positions.items():
3030
for (y1, x1), (y2, x2) in combinations(positions, 2):
3131
anodes_pos |= {
32-
pos for pos in [(y1 + (y1 - y2), x1 + (x1 - x2)), (y2 + (y2 - y1), x2 + (x2 - x1))] if is_in_field(pos)
32+
pos
33+
for pos in [(y1 + (y1 - y2) * mul, x1 + (x1 - x2) * mul), (y2 + (y2 - y1) * mul, x2 + (x2 - x1) * mul)]
34+
if is_in_field(pos)
3335
}
3436
return anodes_pos
3537

@@ -38,9 +40,19 @@ def part1(am: AntennaMap) -> int:
3840
return len(positions(am))
3941

4042

43+
def part2(am: AntennaMap) -> int:
44+
anodes = set()
45+
for i in range(1, max(am.cols, am.rows)):
46+
anodes |= positions(am, i)
47+
for v in am.positions.values():
48+
anodes |= v
49+
return len(anodes)
50+
51+
4152
def main():
4253
lines = parse_input(Path(sys.argv[1]).read_text().splitlines())
4354
print(f"Part 1: {part1(lines)}")
55+
print(f"Part 2: {part2(lines)}")
4456

4557

4658
if __name__ == "__main__":

test/test_day8.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from aoc.day8 import parse_input, part1
1+
from aoc.day8 import parse_input, part1, part2
22

33
TEST_INPUT = """............
44
........0...
@@ -17,3 +17,7 @@
1717

1818
def test_part1():
1919
assert part1(parse_input(TEST_INPUT)) == 14
20+
21+
22+
def test_part2():
23+
assert part2(parse_input(TEST_INPUT)) == 34

0 commit comments

Comments
 (0)