My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add polygon area/perimeter functions to utils

+23 -1
+1 -1
2023/starter.py
··· 7 7 from utils import parse_line, parse_nums, mul, all_unique, factors, memoize, primes, resolve_mapping 8 8 from utils import chunks, parts, gcd, lcm, print_grid, min_max_xy 9 9 from utils import new_table, transposed, rotated, firsts, lasts 10 - from utils import md5, sha256, VOWELS, CONSONANTS, HASH 10 + from utils import md5, sha256, VOWELS, CONSONANTS, HASH, polygon_perimeter, polygon_area 11 11 from utils import Point, DIRS, DIRS_4, DIRS_8, N, NE, E, SE, S, SW, W, NW 12 12 # Itertools Functions: 13 13 # product('ABCD', repeat=2) AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
+22
2023/utils.py
··· 435 435 return (abs(x) + abs(y) + abs(z)) // 2 436 436 437 437 438 + def polygon_perimeter(points): 439 + """Given a set of bounding box points, returns the perimeter of the polygon.""" 440 + return sum(a.dist_manhattan(b) for a, b in zip(points, points[1:] + [points[0]])) 441 + 442 + 443 + def polygon_area(points): 444 + """Given a set of integer bounding box points, returns the total area of the polygon.""" 445 + # Use shoelace formula to compute internal area. 446 + area = 0 447 + 448 + for a, b in zip(points, points[1:] + [points[0]]): 449 + area += (b.x + a.x) * (b.y - a.y) 450 + 451 + area = int(abs(area / 2.0)) 452 + 453 + # Calculate perimeter. 454 + perimeter = polygon_perimeter(points) 455 + 456 + # Account for outer perimeter strip in final area computation. 457 + return area + (perimeter // 2) + 1 458 + 459 + 438 460 @total_ordering 439 461 class Point: 440 462 """Simple 2-dimensional point."""