Skip to content

Commit

Permalink
[2024/12] p2 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 13, 2024
1 parent 45e4f93 commit 959be4d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
Binary file modified .aoc_tiles/tiles/2024/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions 2024/12/example_ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA
File renamed without changes.
5 changes: 5 additions & 0 deletions 2024/12/example_ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
EEEEE
EXXXX
EEEEE
EXXXX
EEEEE
5 changes: 5 additions & 0 deletions 2024/12/example_xo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO
41 changes: 36 additions & 5 deletions 2024/12/script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from GhostyUtils import aoc
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2, Dir
from collections import defaultdict


def area(region: list[Vec2]) -> int:
Expand All @@ -10,19 +11,44 @@ def area(region: list[Vec2]) -> int:
def neighbours(pos: Vec2) -> list[Vec2]:
return [
(pos + Vec2(Dir.UP)).as_tuple(),
(pos + Vec2(Dir.RIGHT)).as_tuple(),
(pos + Vec2(Dir.DOWN)).as_tuple(),
(pos + Vec2(Dir.LEFT)).as_tuple(),
(pos + Vec2(Dir.RIGHT)).as_tuple(),
]


def perimeter(region: list[Vec2]) -> int:
def perimeter(region: set[tuple]) -> int:
p = 0
for pos in region:
p += sum(1 for n in neighbours(pos) if n not in region)
p += sum(n not in region for n in neighbours(pos))
return p


def sides(region: set[tuple]) -> int:
fences = defaultdict(list)
for pos in region:
for n, d in Dir.map_nswe(neighbours(pos)).items():
if n not in region:
fences[d].append(pos)
for d, fs in fences.items():
fences[d] = sorted(fs, key=lambda f: f[::-1] if d in {Dir.N, Dir.S} else f)

num_sides = 0
dirmap = {
Dir.N: Dir.E,
Dir.S: Dir.E,
Dir.W: Dir.S,
Dir.E: Dir.S,
}
for d, fs in fences.items():
num_sides += 1
for f1, f2 in zip(fs, fs[1:]):
if (Vec2(f1) + dirmap[d]).as_tuple() != f2:
num_sides += 1

return num_sides


def main():
grid = Grid(aoc.read_lines())
regions = []
Expand All @@ -37,13 +63,18 @@ def main():
if aoc.args.progress or aoc.args.verbose:
for region in regions:
r = grid[next(iter(region))]
print(f"{r} | area: {area(region)}, perimeter: {perimeter(region)}, "
f"cost: {area(region) * perimeter(region)}")
print(f"{r} | "
f"area: {area(region)}, "
f"perimeter: {perimeter(region)}, "
f"sides: {sides(region)} | "
f"p1 cost: {area(region) * perimeter(region)} | "
f"p2 cost: {area(region) * sides(region)}")
if aoc.args.verbose:
print(f"> {region}")
print(f"{len(regions)} regions")

print(f"p1: {sum(area(region) * perimeter(region) for region in regions)}")
print(f"p2: {sum(area(region) * sides(region) for region in regions)}")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 184/474
Advent of Code - 185/476
</h1>
<h1 align="center">
2024 - 23 ⭐ - Python
2024 - 24 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down

0 comments on commit 959be4d

Please sign in to comment.