summaryrefslogtreecommitdiffstats
path: root/aoc/datastructures.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-19 09:30:43 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-19 09:30:43 +0100
commitf3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2 (patch)
tree535a7048c7547e1eaef6f97dcfd8b2cad598ca50 /aoc/datastructures.py
parent06cb539f69f0b501afaa9ef5b6d89863e1c9d111 (diff)
download2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.tar.gz
2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.tar.bz2
2022-f3d0899dbfd0aa3e6aebf5d19ec89d58ead418b2.zip
Day 18
Diffstat (limited to 'aoc/datastructures.py')
-rw-r--r--aoc/datastructures.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/aoc/datastructures.py b/aoc/datastructures.py
index 81a68e4..244aa1e 100644
--- a/aoc/datastructures.py
+++ b/aoc/datastructures.py
@@ -1,5 +1,6 @@
1# -*- coding: utf-8 -*- 1# -*- coding: utf-8 -*-
2from collections import namedtuple 2from collections import namedtuple
3from typing import Iterator
3 4
4 5
5class Coordinate(namedtuple("Coordinate", ["x", "y"])): 6class Coordinate(namedtuple("Coordinate", ["x", "y"])):
@@ -28,3 +29,70 @@ class Coordinate(namedtuple("Coordinate", ["x", "y"])):
28 px, 29 px,
29 py, 30 py,
30 ) 31 )
32
33 def neighbours(self, no_diagonal: bool = False) -> Iterator["Coordinate"]:
34 if no_diagonal:
35 yield self + Coordinate(-1, 0),
36 yield self + Coordinate(1, 0),
37 yield self + Coordinate(0, -1),
38 yield self + Coordinate(0, 1),
39 else:
40 for dy in range(self.y - 1, self.y + 2):
41 for dx in range(self.x - 1, self.x + 2):
42 if dy == self.y and dx == self.x:
43 continue
44
45 yield Coordinate(dx, dy)
46
47
48class Coordinate3(namedtuple("Coordinate3", ["x", "y", "z"])):
49 def __sub__(self, other: "Coordinate3") -> "Coordinate3":
50 return Coordinate3(self.x - other.x, self.y - other.y, self.z - other.z)
51
52 def __add__(self, other: "Coordinate3") -> "Coordinate3":
53 return Coordinate3(self.x + other.x, self.y + other.y, self.z + other.z)
54
55 def manhattan_distance(self, other: "Coordinate3") -> int:
56 return abs(self.x - other.x) + abs(self.y - other.y) + abs(self.z - other.z)
57
58 @property
59 def polarity(self) -> "Coordinate3":
60 try:
61 px = abs(self.x) / self.x
62 except ZeroDivisionError:
63 px = 0
64
65 try:
66 py = abs(self.y) / self.y
67 except ZeroDivisionError:
68 py = 0
69
70 try:
71 pz = abs(self.z) / self.z
72 except ZeroDivisionError:
73 pz = 0
74
75 return Coordinate3(
76 px,
77 py,
78 pz,
79 )
80
81 def neighbours(self, no_diagonal: bool = False) -> Iterator["Coordinate3"]:
82 if no_diagonal:
83 yield self + Coordinate3(-1, 0, 0)
84 yield self + Coordinate3(1, 0, 0)
85 yield self + Coordinate3(0, -1, 0)
86 yield self + Coordinate3(0, 1, 0)
87 yield self + Coordinate3(0, 0, -1)
88 yield self + Coordinate3(0, 0, 1)
89 else:
90 for dz in range(self.z - 1, self.z + 2):
91 for dy in range(self.y - 1, self.y + 2):
92 for dx in range(self.x - 1, self.x + 2):
93 coordinate = Coordinate3(dx, dy, dz)
94
95 if dy == self.y and dx == self.x:
96 continue
97
98 yield coordinate