Skip to content

Commit c53f08b

Browse files
committed
completed part 1 of day 12 of year 2024
1 parent 22ef911 commit c53f08b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

2024/12.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
def parse(data):
5+
grid = {}
6+
bounds = len(data.split('\n')[0]), len(data.split('\n'))
7+
for y, line in enumerate(data.split('\n')):
8+
for x, char in enumerate(line):
9+
grid[(x, y)] = char
10+
11+
return grid, bounds
12+
13+
split_data = parse
14+
completed = 1
15+
raw_data = None # Not To be touched
16+
17+
def part1(data):
18+
grid, bounds = data
19+
price = 0
20+
21+
def fencing(anchor):
22+
x, y = anchor
23+
area = 1
24+
perimeter = 0
25+
patch = set([(x, y)])
26+
crop = grid[anchor]
27+
movement = [(1, 0), (-1, 0), (0, 1), (0, -1)]
28+
queue = [anchor]
29+
30+
while queue:
31+
x, y = queue.pop(0)
32+
for dx, dy in movement:
33+
nx, ny = x + dx, y + dy
34+
if not (0 <= nx < bounds[0] and 0 <= ny < bounds[1]):
35+
perimeter += 1 # The border at the edge of the map
36+
elif (nx, ny) in patch:
37+
continue # Already seen this
38+
elif grid[(nx, ny)] == crop:
39+
area += 1
40+
queue.append((nx, ny))
41+
patch.add((nx, ny))
42+
else:
43+
perimeter += 1
44+
45+
return area, perimeter, patch
46+
47+
seen = set()
48+
49+
for y in range(bounds[1]):
50+
for x in range(bounds[0]):
51+
if (x, y) in seen: continue
52+
area, perimeter, patch = fencing((x, y))
53+
# print(f'A region of {grid[(x, y)]} plants with price {area} * {perimeter} = {area*perimeter}')
54+
seen.update(patch)
55+
price += area*perimeter
56+
57+
return price
58+
59+
def part2(data):
60+
grid, bounds = data
61+
...

0 commit comments

Comments
 (0)