Skip to content

Commit 959be4d

Browse files
committed
[2024/12] p2 solved
1 parent 45e4f93 commit 959be4d

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

.aoc_tiles/tiles/2024/12.png

44 Bytes
Loading

2024/12/example_ab

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
AAAAAA
2+
AAABBA
3+
AAABBA
4+
ABBAAA
5+
ABBAAA
6+
AAAAAA
File renamed without changes.

2024/12/example_ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
EEEEE
2+
EXXXX
3+
EEEEE
4+
EXXXX
5+
EEEEE

2024/12/example_xo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OOOOO
2+
OXOXO
3+
OOOOO
4+
OXOXO
5+
OOOOO

2024/12/script.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from GhostyUtils import aoc
22
from GhostyUtils.grid import Grid
33
from GhostyUtils.vec2 import Vec2, Dir
4+
from collections import defaultdict
45

56

67
def area(region: list[Vec2]) -> int:
@@ -10,19 +11,44 @@ def area(region: list[Vec2]) -> int:
1011
def neighbours(pos: Vec2) -> list[Vec2]:
1112
return [
1213
(pos + Vec2(Dir.UP)).as_tuple(),
13-
(pos + Vec2(Dir.RIGHT)).as_tuple(),
1414
(pos + Vec2(Dir.DOWN)).as_tuple(),
1515
(pos + Vec2(Dir.LEFT)).as_tuple(),
16+
(pos + Vec2(Dir.RIGHT)).as_tuple(),
1617
]
1718

1819

19-
def perimeter(region: list[Vec2]) -> int:
20+
def perimeter(region: set[tuple]) -> int:
2021
p = 0
2122
for pos in region:
22-
p += sum(1 for n in neighbours(pos) if n not in region)
23+
p += sum(n not in region for n in neighbours(pos))
2324
return p
2425

2526

27+
def sides(region: set[tuple]) -> int:
28+
fences = defaultdict(list)
29+
for pos in region:
30+
for n, d in Dir.map_nswe(neighbours(pos)).items():
31+
if n not in region:
32+
fences[d].append(pos)
33+
for d, fs in fences.items():
34+
fences[d] = sorted(fs, key=lambda f: f[::-1] if d in {Dir.N, Dir.S} else f)
35+
36+
num_sides = 0
37+
dirmap = {
38+
Dir.N: Dir.E,
39+
Dir.S: Dir.E,
40+
Dir.W: Dir.S,
41+
Dir.E: Dir.S,
42+
}
43+
for d, fs in fences.items():
44+
num_sides += 1
45+
for f1, f2 in zip(fs, fs[1:]):
46+
if (Vec2(f1) + dirmap[d]).as_tuple() != f2:
47+
num_sides += 1
48+
49+
return num_sides
50+
51+
2652
def main():
2753
grid = Grid(aoc.read_lines())
2854
regions = []
@@ -37,13 +63,18 @@ def main():
3763
if aoc.args.progress or aoc.args.verbose:
3864
for region in regions:
3965
r = grid[next(iter(region))]
40-
print(f"{r} | area: {area(region)}, perimeter: {perimeter(region)}, "
41-
f"cost: {area(region) * perimeter(region)}")
66+
print(f"{r} | "
67+
f"area: {area(region)}, "
68+
f"perimeter: {perimeter(region)}, "
69+
f"sides: {sides(region)} | "
70+
f"p1 cost: {area(region) * perimeter(region)} | "
71+
f"p2 cost: {area(region) * sides(region)}")
4272
if aoc.args.verbose:
4373
print(f"> {region}")
4474
print(f"{len(regions)} regions")
4575

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

4879

4980
if __name__ == "__main__":

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 184/474
6+
Advent of Code - 185/476
77
</h1>
88
<h1 align="center">
9-
2024 - 23 ⭐ - Python
9+
2024 - 24 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">

0 commit comments

Comments
 (0)