diff options
author | mhsn <mail@mhsn.net> | 2024-12-12 10:23:28 +0000 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2024-12-12 10:23:28 +0000 |
commit | b6f8fa4e66c3ea8f1f090a3fbec12b94dc5cd858 (patch) | |
tree | 25476ad63021e595a7a7b49486ccb3b706b25461 | |
parent | 8ea562f5d1d76cf9c9e2ef80560e72c32104c3fb (diff) | |
download | aoc-b6f8fa4e66c3ea8f1f090a3fbec12b94dc5cd858.tar.gz aoc-b6f8fa4e66c3ea8f1f090a3fbec12b94dc5cd858.zip |
2024-12 python p1
-rw-r--r-- | 2024/12/python/main.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/2024/12/python/main.py b/2024/12/python/main.py new file mode 100644 index 0000000..6faa6f1 --- /dev/null +++ b/2024/12/python/main.py @@ -0,0 +1,47 @@ +from fileinput import input + +grid = { + complex(idx, idy): c + for idy, line in enumerate(input()) + for idx, c in enumerate(line.strip()) +} +xmax = int(max(x.real for x in grid.keys())) +ymax = int(max(x.imag for x in grid.keys())) + +seen = set() + +silver = 0 + +for x in range(xmax + 1): + for y in range(ymax + 1): + c = complex(x, y) + if c in seen: + continue + + # Flood fill + char = grid[c] + perimeter = area = 0 + queue = [c] + while queue: + curr = queue.pop() + if curr in seen: + continue + seen.add(curr) + + for dir in [1, -1, 1j, -1j]: + next_ = curr + dir + if ( + not (0 <= next_.real <= xmax and 0 <= next_.imag <= ymax) + or grid[next_] != char + ): + perimeter += 1 + continue + + queue.append(next_) + area += 1 + silver += perimeter * area + +gold = 0 + +print("silver:", silver) +print("gold:", gold) |